forked from mrchuanxu/RegularNotes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinaryTree_Travel.js
45 lines (45 loc) · 1.2 KB
/
BinaryTree_Travel.js
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
40
41
42
43
44
45
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {number[][]}
*/
var levelOrder = function(root) {
// 这个用队列实现
if(root==null) return [];
var res = [];
var que_pre = [];
var que_nxt = [];
que_pre.push(root);
var res_tmp = [];
var isNxt = 1;
while(que_pre.length||que_nxt.length){
if(isNxt){
var tmpNode = que_pre.shift();
if(tmpNode.left!=null) que_nxt.push(tmpNode.left);
if(tmpNode.right!=null)que_nxt.push(tmpNode.right);
res_tmp.push(tmpNode.val);
if(que_pre.length==0){
isNxt = 0;
res.push(res_tmp);
res_tmp = [];
}
}else{
var tmpNode = que_nxt.shift();
if(tmpNode.left!=null) que_pre.push(tmpNode.left);
if(tmpNode.right!=null)que_pre.push(tmpNode.right);
res_tmp.push(tmpNode.val);
if(que_nxt.length==0){
isNxt = 1;
res.push(res_tmp);
res_tmp = [];
}
}
}
return res;
};