Skip to content

Commit 99d4506

Browse files
committed
Add solution #107
1 parent f064fdc commit 99d4506

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@
112112
104|[Maximum Depth of Binary Tree](./0104-maximum-depth-of-binary-tree.js)|Easy|
113113
105|[Construct Binary Tree from Preorder and Inorder Traversal](./0105-construct-binary-tree-from-preorder-and-inorder-traversal.js)|Medium|
114114
106|[Construct Binary Tree from Inorder and Postorder Traversal](./0106-construct-binary-tree-from-inorder-and-postorder-traversal.js)|Medium|
115+
107|[Binary Tree Level Order Traversal II](./0107-binary-tree-level-order-traversal-ii.js)|Medium|
115116
108|[Convert Sorted Array to Binary Search Tree](./0108-convert-sorted-array-to-binary-search-tree.js)|Easy|
116117
110|[Balanced Binary Tree](./0110-balanced-binary-tree.js)|Easy|
117118
111|[Minimum Depth of Binary Tree](./0111-minimum-depth-of-binary-tree.js)|Easy|
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* 107. Binary Tree Level Order Traversal II
3+
* https://leetcode.com/problems/binary-tree-level-order-traversal-ii/
4+
* Difficulty: Medium
5+
*
6+
* Given the root of a binary tree, return the bottom-up level order traversal of its nodes' values.
7+
* (i.e., from left to right, level by level from leaf to root).
8+
*/
9+
10+
/**
11+
* Definition for a binary tree node.
12+
* function TreeNode(val, left, right) {
13+
* this.val = (val===undefined ? 0 : val)
14+
* this.left = (left===undefined ? null : left)
15+
* this.right = (right===undefined ? null : right)
16+
* }
17+
*/
18+
/**
19+
* @param {TreeNode} root
20+
* @return {number[][]}
21+
*/
22+
var levelOrderBottom = function(root) {
23+
if (!root) return [];
24+
25+
const result = [];
26+
const stack = [root];
27+
28+
while (stack.length > 0) {
29+
const level = [];
30+
const n = stack.length;
31+
32+
for (let i = 0; i < n; i++) {
33+
const node = stack.shift();
34+
level.push(node.val);
35+
36+
if (node.left) stack.push(node.left);
37+
if (node.right) stack.push(node.right);
38+
}
39+
40+
result.unshift(level);
41+
}
42+
43+
return result;
44+
};

0 commit comments

Comments
 (0)