File tree Expand file tree Collapse file tree 2 files changed +45
-0
lines changed Expand file tree Collapse file tree 2 files changed +45
-0
lines changed Original file line number Diff line number Diff line change 112
112
104|[ Maximum Depth of Binary Tree] ( ./0104-maximum-depth-of-binary-tree.js ) |Easy|
113
113
105|[ Construct Binary Tree from Preorder and Inorder Traversal] ( ./0105-construct-binary-tree-from-preorder-and-inorder-traversal.js ) |Medium|
114
114
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|
115
116
108|[ Convert Sorted Array to Binary Search Tree] ( ./0108-convert-sorted-array-to-binary-search-tree.js ) |Easy|
116
117
110|[ Balanced Binary Tree] ( ./0110-balanced-binary-tree.js ) |Easy|
117
118
111|[ Minimum Depth of Binary Tree] ( ./0111-minimum-depth-of-binary-tree.js ) |Easy|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments