Skip to content

Commit 9a972b9

Browse files
authored
Create BinaryTreePruning.java
1 parent afe97b6 commit 9a972b9

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

BinaryTreePruning.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
16+
class Solution {
17+
public TreeNode pruneTree(TreeNode root) {
18+
19+
if (root == null) return null;
20+
21+
root.left = pruneTree(root.left);
22+
root.right = pruneTree(root.right);
23+
24+
if (root.left == null && root.right == null && root.val == 0) return null;
25+
26+
return root;
27+
28+
}
29+
}

0 commit comments

Comments
 (0)