File tree 5 files changed +73
-0
lines changed
algorithms/BinaryTreePruning
5 files changed +73
-0
lines changed Original file line number Diff line number Diff line change @@ -265,6 +265,7 @@ All solutions will be accepted!
265
265
| 885| [ Spiral Matrix III] ( https://leetcode-cn.com/problems/spiral-matrix-iii/description/ ) | [ java/py/js] ( ./algorithms/SpiralMatrixIII ) | Medium|
266
266
| 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|
267
267
| 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|
268
269
269
270
# Database
270
271
| #| Title| Solution| Difficulty|
Original file line number Diff line number Diff line change
1
+ # Binary Tree Pruning
2
+ We can solve this problem by recursive
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments