File tree Expand file tree Collapse file tree 2 files changed +44
-0
lines changed Expand file tree Collapse file tree 2 files changed +44
-0
lines changed Original file line number Diff line number Diff line change 354
354
363|[ Max Sum of Rectangle No Larger Than K] ( ./solutions/0363-max-sum-of-rectangle-no-larger-than-k.js ) |Hard|
355
355
364|[ Nested List Weight Sum II] ( ./solutions/0364-nested-list-weight-sum-ii.js ) |Medium|
356
356
365|[ Water and Jug Problem] ( ./solutions/0365-water-and-jug-problem.js ) |Medium|
357
+ 366|[ Find Leaves of Binary Tree] ( ./solutions/0366-find-leaves-of-binary-tree.js ) |Medium|
357
358
367|[ Valid Perfect Square] ( ./solutions/0367-valid-perfect-square.js ) |Easy|
358
359
368|[ Largest Divisible Subset] ( ./solutions/0368-largest-divisible-subset.js ) |Medium|
359
360
371|[ Sum of Two Integers] ( ./solutions/0371-sum-of-two-integers.js ) |Medium|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 366. Find Leaves of Binary Tree
3
+ * https://leetcode.com/problems/find-leaves-of-binary-tree/
4
+ * Difficulty: Medium
5
+ *
6
+ * Given the root of a binary tree, collect a tree's nodes as if you were doing this:
7
+ * - Collect all the leaf nodes.
8
+ * - Remove all the leaf nodes.
9
+ * - Repeat until the tree is empty.
10
+ */
11
+
12
+ /**
13
+ * Definition for a binary tree node.
14
+ * function TreeNode(val, left, right) {
15
+ * this.val = (val===undefined ? 0 : val)
16
+ * this.left = (left===undefined ? null : left)
17
+ * this.right = (right===undefined ? null : right)
18
+ * }
19
+ */
20
+ /**
21
+ * @param {TreeNode } root
22
+ * @return {number[][] }
23
+ */
24
+ var findLeaves = function ( root ) {
25
+ const levels = [ ] ;
26
+ getHeight ( root ) ;
27
+ return levels ;
28
+
29
+ function getHeight ( node ) {
30
+ if ( ! node ) return - 1 ;
31
+
32
+ const leftHeight = getHeight ( node . left ) ;
33
+ const rightHeight = getHeight ( node . right ) ;
34
+ const height = Math . max ( leftHeight , rightHeight ) + 1 ;
35
+
36
+ if ( levels . length <= height ) {
37
+ levels . push ( [ ] ) ;
38
+ }
39
+ levels [ height ] . push ( node . val ) ;
40
+
41
+ return height ;
42
+ }
43
+ } ;
You can’t perform that action at this time.
0 commit comments