Skip to content

Commit e744ed7

Browse files
committed
Add solution #814
1 parent 7cb770c commit e744ed7

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,7 @@
622622
811|[Subdomain Visit Count](./0811-subdomain-visit-count.js)|Medium|
623623
812|[Largest Triangle Area](./0812-largest-triangle-area.js)|Easy|
624624
813|[Largest Sum of Averages](./0813-largest-sum-of-averages.js)|Medium|
625+
814|[Binary Tree Pruning](./0814-binary-tree-pruning.js)|Medium|
625626
819|[Most Common Word](./0819-most-common-word.js)|Easy|
626627
821|[Shortest Distance to a Character](./0821-shortest-distance-to-a-character.js)|Easy|
627628
824|[Goat Latin](./0824-goat-latin.js)|Easy|

Diff for: solutions/0814-binary-tree-pruning.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
};

0 commit comments

Comments
 (0)