Skip to content

Commit 23532a8

Browse files
committed
Binary tree level order traversal II
1 parent ae172c1 commit 23532a8

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
* @param {TreeNode} root
10+
* @return {number[][]}
11+
*/
12+
var levelOrderBottom = function(root) {
13+
if(root.left === null && root.right === null) {
14+
return [[root]];
15+
} else {
16+
var lastLevel = [root];
17+
var ret = [root];
18+
19+
var i=0;
20+
while(lastLevel) {
21+
var len = lastLevel.length;
22+
var level = [];
23+
for(;i<len;i++) {
24+
if(lastLevel[i].val !== '#') {
25+
level.push(lastLevel.left);
26+
level.push(lastLevel.right);
27+
i+=2;
28+
}
29+
}
30+
ret.unshift(level);
31+
lastLevel = level;
32+
}
33+
}
34+
};

0 commit comments

Comments
 (0)