-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path103.js
More file actions
39 lines (38 loc) · 924 Bytes
/
103.js
File metadata and controls
39 lines (38 loc) · 924 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number[][]}
*/
var zigzagLevelOrder = function (root) {
if (root === null) return []
let q = []
let res = []
let level = 1
q.push(root)
while (q.length) {
let len = q.length
let subRes = []
while (len) {
let node = q.shift()
level % 2 ? subRes.push(node.val) : subRes.unshift(node.val)
node.left ? q.push(node.left) : ''
node.right ? q.push(node.right) : ''
len--
}
level++
res.push(subRes)
}
return res
}
/*
2021/9/26
57 13
bfs遍历,通过层数控制每层子数组的顺序
*/