File tree 2 files changed +33
-0
lines changed
2 files changed +33
-0
lines changed Original file line number Diff line number Diff line change 622
622
811|[ Subdomain Visit Count] ( ./0811-subdomain-visit-count.js ) |Medium|
623
623
812|[ Largest Triangle Area] ( ./0812-largest-triangle-area.js ) |Easy|
624
624
813|[ Largest Sum of Averages] ( ./0813-largest-sum-of-averages.js ) |Medium|
625
+ 814|[ Binary Tree Pruning] ( ./0814-binary-tree-pruning.js ) |Medium|
625
626
819|[ Most Common Word] ( ./0819-most-common-word.js ) |Easy|
626
627
821|[ Shortest Distance to a Character] ( ./0821-shortest-distance-to-a-character.js ) |Easy|
627
628
824|[ Goat Latin] ( ./0824-goat-latin.js ) |Easy|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 814. Binary Tree Pruning
3
+ * https://leetcode.com/problems/binary-tree-pruning/
4
+ * Difficulty: Medium
5
+ *
6
+ * Given the root of a binary tree, return the same tree where every subtree (of the given tree)
7
+ * not containing a 1 has been removed.
8
+ *
9
+ * A subtree of a node node is node plus every node that is a descendant of node.
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 {TreeNode }
23
+ */
24
+ var pruneTree = function ( root ) {
25
+ if ( ! root ) return null ;
26
+ root . left = pruneTree ( root . left ) ;
27
+ root . right = pruneTree ( root . right ) ;
28
+ if ( ! root . left && ! root . right && root . val === 0 ) {
29
+ return null ;
30
+ }
31
+ return root ;
32
+ } ;
You can’t perform that action at this time.
0 commit comments