Skip to content

Commit dd5f7e7

Browse files
authored
Update 107-binary-tree-level-order-traversal-ii.js
1 parent e144953 commit dd5f7e7

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

107-binary-tree-level-order-traversal-ii.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,23 @@ const levelOrderBottom = function(root) {
2323
}
2424
}
2525
}
26+
27+
// another
28+
29+
const levelOrderBottom = function(root) {
30+
if (!root) return []
31+
const currentLevelNodes = [root]
32+
const result = []
33+
while (currentLevelNodes.length > 0) {
34+
const count = currentLevelNodes.length
35+
const currentLevelValues = []
36+
for (let i = 0; i < count; i++) {
37+
const node = currentLevelNodes.shift()
38+
currentLevelValues.push(node.val)
39+
if (node.left) currentLevelNodes.push(node.left)
40+
if (node.right) currentLevelNodes.push(node.right)
41+
}
42+
result.unshift(currentLevelValues)
43+
}
44+
return result
45+
}

0 commit comments

Comments
 (0)