Skip to content

Commit e11919a

Browse files
committed
solve problem Binary Tree Pruning
1 parent 75bda6f commit e11919a

File tree

5 files changed

+73
-0
lines changed

5 files changed

+73
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ All solutions will be accepted!
265265
|885|[Spiral Matrix III](https://leetcode-cn.com/problems/spiral-matrix-iii/description/)|[java/py/js](./algorithms/SpiralMatrixIII)|Medium|
266266
|19|[Remove Nth Node From End Of List](https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/description/)|[java/py/js](./algorithms/RemoveNthNodeFromEndOfList)|Medium|
267267
|526|[Beautiful Arrangement](https://leetcode-cn.com/problems/beautiful-arrangement/description/)|[java/py/js](./algorithms/BeautifulArrangement)|Medium|
268+
|814|[Binary Tree Pruning](https://leetcode-cn.com/problems/binary-tree-pruning/description/)|[java/py/js](./algorithms/BinaryTreePruning)|Medium|
268269

269270
# Database
270271
|#|Title|Solution|Difficulty|
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Binary Tree Pruning
2+
We can solve this problem by recursive
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
class Solution {
11+
public TreeNode pruneTree(TreeNode root) {
12+
root.left = root.left != null ? helper(root.left) : null;
13+
root.right = root.right != null ? helper(root.right) : null;
14+
return root;
15+
}
16+
17+
public TreeNode helper(TreeNode root) {
18+
root.left = root.left != null ? helper(root.left) : null;
19+
root.right = root.right != null ? helper(root.right) : null;
20+
21+
return (root.left != null || root.right != null || root.val == 1) ? root : null;
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
* @param {TreeNode} root
10+
* @return {TreeNode}
11+
*/
12+
var pruneTree = function(root) {
13+
root.left = root.left ? helper(root.left) : null
14+
root.right = root.right ? helper(root.right) : null
15+
return root
16+
};
17+
18+
var helper = function(root) {
19+
root.left = root.left ? helper(root.left) : null
20+
root.right = root.right ? helper(root.right) : null
21+
22+
return (root.left || root.right || root.val == 1) ? root : null
23+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution(object):
9+
def pruneTree(self, root):
10+
"""
11+
:type root: TreeNode
12+
:rtype: TreeNode
13+
"""
14+
self.helper(root)
15+
return root
16+
17+
def helper(self, root):
18+
left_flag = False if not root.left else self.helper(root.left)
19+
right_flag = False if not root.right else self.helper(root.right)
20+
if not left_flag:
21+
root.left = None
22+
if not right_flag:
23+
root.right = None
24+
return root.val == 1 or left_flag or right_flag

0 commit comments

Comments
 (0)