Skip to content

Commit 5765f61

Browse files
committed
feat: add solutions
1 parent 34903dd commit 5765f61

File tree

6 files changed

+57
-29
lines changed

6 files changed

+57
-29
lines changed

solution/0800-0899/0814.Binary Tree Pruning/README.md

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,13 @@ tags:
6464

6565
<!-- solution:start -->
6666

67-
### 方法一:DFS
67+
### 方法一:递归
6868

69-
观察叶节点,当叶节点 `val` 为 0 时,便将该节点抹去。回溯,查看其父节点是否成为了新的叶节点,依照此规则自底向上。
69+
我们首先判断当前节点是否为空,如果为空则直接返回空节点。
70+
71+
否则,我们递归地对左右子树进行剪枝,并将剪枝后的左右子树重新赋值给当前节点的左右子节点。然后判断当前节点的值是否为 0 且左右子节点都为空,如果是则返回空节点,否则返回当前节点。
72+
73+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为二叉树的节点个数。
7074

7175
<!-- tabs:start -->
7276

@@ -82,10 +86,10 @@ tags:
8286
class Solution:
8387
def pruneTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
8488
if root is None:
85-
return None
89+
return root
8690
root.left = self.pruneTree(root.left)
8791
root.right = self.pruneTree(root.right)
88-
if root.val == 0 and root.left is None and root.right is None:
92+
if root.val == 0 and root.left == root.right:
8993
return None
9094
return root
9195
```
@@ -140,10 +144,14 @@ class Solution {
140144
class Solution {
141145
public:
142146
TreeNode* pruneTree(TreeNode* root) {
143-
if (!root) return nullptr;
147+
if (!root) {
148+
return root;
149+
}
144150
root->left = pruneTree(root->left);
145151
root->right = pruneTree(root->right);
146-
if (!root->val && !root->left && !root->right) return nullptr;
152+
if (root->val == 0 && root->left == root->right) {
153+
return nullptr;
154+
}
147155
return root;
148156
}
149157
};
@@ -191,12 +199,12 @@ func pruneTree(root *TreeNode) *TreeNode {
191199
*/
192200

193201
function pruneTree(root: TreeNode | null): TreeNode | null {
194-
if (root == null) {
202+
if (!root) {
195203
return root;
196204
}
197205
root.left = pruneTree(root.left);
198206
root.right = pruneTree(root.right);
199-
if (root.val == 0 && root.left == null && root.right == null) {
207+
if (root.val === 0 && root.left === root.right) {
200208
return null;
201209
}
202210
return root;
@@ -262,10 +270,12 @@ impl Solution {
262270
* @return {TreeNode}
263271
*/
264272
var pruneTree = function (root) {
265-
if (!root) return null;
273+
if (!root) {
274+
return root;
275+
}
266276
root.left = pruneTree(root.left);
267277
root.right = pruneTree(root.right);
268-
if (root.val == 0 && !root.left && !root.right) {
278+
if (root.val === 0 && root.left === root.right) {
269279
return null;
270280
}
271281
return root;

solution/0800-0899/0814.Binary Tree Pruning/README_EN.md

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ tags:
2828
<pre>
2929
<strong>Input:</strong> root = [1,null,0,0,1]
3030
<strong>Output:</strong> [1,null,0,null,1]
31-
<strong>Explanation:</strong>
31+
<strong>Explanation:</strong>
3232
Only the red nodes satisfy the property &quot;every subtree not containing a 1&quot;.
3333
The diagram on the right represents the answer.
3434
</pre>
@@ -61,7 +61,13 @@ The diagram on the right represents the answer.
6161

6262
<!-- solution:start -->
6363

64-
### Solution 1
64+
### Solution 1: Recursion
65+
66+
First, we check if the current node is null. If it is, we directly return the null node.
67+
68+
Otherwise, we recursively prune the left and right subtrees and reassign the pruned subtrees to the current node's left and right children. Then, we check if the current node's value is 0 and both its left and right children are null. If so, we return the null node; otherwise, we return the current node.
69+
70+
Time complexity is $O(n)$, and space complexity is $O(n)$. Here, $n$ is the number of nodes in the binary tree.
6571

6672
<!-- tabs:start -->
6773

@@ -77,10 +83,10 @@ The diagram on the right represents the answer.
7783
class Solution:
7884
def pruneTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
7985
if root is None:
80-
return None
86+
return root
8187
root.left = self.pruneTree(root.left)
8288
root.right = self.pruneTree(root.right)
83-
if root.val == 0 and root.left is None and root.right is None:
89+
if root.val == 0 and root.left == root.right:
8490
return None
8591
return root
8692
```
@@ -135,10 +141,14 @@ class Solution {
135141
class Solution {
136142
public:
137143
TreeNode* pruneTree(TreeNode* root) {
138-
if (!root) return nullptr;
144+
if (!root) {
145+
return root;
146+
}
139147
root->left = pruneTree(root->left);
140148
root->right = pruneTree(root->right);
141-
if (!root->val && !root->left && !root->right) return nullptr;
149+
if (root->val == 0 && root->left == root->right) {
150+
return nullptr;
151+
}
142152
return root;
143153
}
144154
};
@@ -186,12 +196,12 @@ func pruneTree(root *TreeNode) *TreeNode {
186196
*/
187197

188198
function pruneTree(root: TreeNode | null): TreeNode | null {
189-
if (root == null) {
199+
if (!root) {
190200
return root;
191201
}
192202
root.left = pruneTree(root.left);
193203
root.right = pruneTree(root.right);
194-
if (root.val == 0 && root.left == null && root.right == null) {
204+
if (root.val === 0 && root.left === root.right) {
195205
return null;
196206
}
197207
return root;
@@ -257,10 +267,12 @@ impl Solution {
257267
* @return {TreeNode}
258268
*/
259269
var pruneTree = function (root) {
260-
if (!root) return null;
270+
if (!root) {
271+
return root;
272+
}
261273
root.left = pruneTree(root.left);
262274
root.right = pruneTree(root.right);
263-
if (root.val == 0 && !root.left && !root.right) {
275+
if (root.val === 0 && root.left === root.right) {
264276
return null;
265277
}
266278
return root;

solution/0800-0899/0814.Binary Tree Pruning/Solution.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,14 @@
1212
class Solution {
1313
public:
1414
TreeNode* pruneTree(TreeNode* root) {
15-
if (!root) return nullptr;
15+
if (!root) {
16+
return root;
17+
}
1618
root->left = pruneTree(root->left);
1719
root->right = pruneTree(root->right);
18-
if (!root->val && !root->left && !root->right) return nullptr;
20+
if (root->val == 0 && root->left == root->right) {
21+
return nullptr;
22+
}
1923
return root;
2024
}
21-
};
25+
};

solution/0800-0899/0814.Binary Tree Pruning/Solution.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
* @return {TreeNode}
1212
*/
1313
var pruneTree = function (root) {
14-
if (!root) return null;
14+
if (!root) {
15+
return root;
16+
}
1517
root.left = pruneTree(root.left);
1618
root.right = pruneTree(root.right);
17-
if (root.val == 0 && !root.left && !root.right) {
19+
if (root.val === 0 && root.left === root.right) {
1820
return null;
1921
}
2022
return root;

solution/0800-0899/0814.Binary Tree Pruning/Solution.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
class Solution:
88
def pruneTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
99
if root is None:
10-
return None
10+
return root
1111
root.left = self.pruneTree(root.left)
1212
root.right = self.pruneTree(root.right)
13-
if root.val == 0 and root.left is None and root.right is None:
13+
if root.val == 0 and root.left == root.right:
1414
return None
1515
return root

solution/0800-0899/0814.Binary Tree Pruning/Solution.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313
*/
1414

1515
function pruneTree(root: TreeNode | null): TreeNode | null {
16-
if (root == null) {
16+
if (!root) {
1717
return root;
1818
}
1919
root.left = pruneTree(root.left);
2020
root.right = pruneTree(root.right);
21-
if (root.val == 0 && root.left == null && root.right == null) {
21+
if (root.val === 0 && root.left === root.right) {
2222
return null;
2323
}
2424
return root;

0 commit comments

Comments
 (0)