-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryTreePruning.java
38 lines (33 loc) · 959 Bytes
/
BinaryTreePruning.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package binarytree;
import binarytree.BinaryTreeTest.TreeNode;
/**
* @author Shogo Akiyama
* Solved on 12/20/2019
*
* 814. Binary Tree Pruning
* https://leetcode.com/problems/binary-tree-pruning/
* Difficulty: Medium
*
* Approach: Recursion
* Runtime: 0 ms, faster than 100.00% of Java online submissions for Binary Tree Pruning.
* Memory Usage: 34.6 MB, less than 100.00% of Java online submissions for Binary Tree Pruning.
*
* Time Complexity: O(n)
* Space Complexity: O(d) for the recursive stack
* Where n is the number of nodes in a tree and d is the maximum depth of the tree
*
* @see BinaryTreeTest#testBinaryTreePruning()
*/
public class BinaryTreePruning {
public TreeNode pruneTree(TreeNode root) {
if (root == null) {
return null;
}
root.left = pruneTree(root.left);
root.right = pruneTree(root.right);
if (root.left == null && root.right == null && root.val == 0) {
return null;
}
return root;
}
}