Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4dd8462

Browse files
feat: add solutions to lc/lcof2 problem: Binary Tree Pruning
1 parent 748d1b6 commit 4dd8462

File tree

9 files changed

+321
-12
lines changed

9 files changed

+321
-12
lines changed

‎lcof2/剑指 Offer II 047. 二叉树剪枝/README.md‎

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
<pre>
1616
<strong>输入:</strong> [1,null,0,0,1]
17-
<strong>输出: </strong>[1,null,0,null,1]
18-
<strong>解释:</strong>
17+
<strong>输出: </strong>[1,null,0,null,1]
18+
<strong>解释:</strong>
1919
只有红色节点满足条件&ldquo;所有不包含 1 的子树&rdquo;
2020
右图为返回的答案。
2121

@@ -27,7 +27,7 @@
2727
<pre>
2828
<strong>输入:</strong> [1,0,1,0,0,0,1]
2929
<strong>输出: </strong>[1,null,1,null,1]
30-
<strong>解释:</strong>
30+
<strong>解释:</strong>
3131

3232
<img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/lcof2/%E5%89%91%E6%8C%87%20Offer%20II%20047.%20%E4%BA%8C%E5%8F%89%E6%A0%91%E5%89%AA%E6%9E%9D/images/1028_1.png" style="width:450px" />
3333
</pre>
@@ -37,7 +37,7 @@
3737
<pre>
3838
<strong>输入:</strong> [1,1,0,1,1,0,1,0]
3939
<strong>输出: </strong>[1,1,0,1,1,null,1]
40-
<strong>解释:</strong>
40+
<strong>解释:</strong>
4141

4242
<img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/lcof2/%E5%89%91%E6%8C%87%20Offer%20II%20047.%20%E4%BA%8C%E5%8F%89%E6%A0%91%E5%89%AA%E6%9E%9D/images/1028.png" style="width:450px" />
4343
</pre>
@@ -67,15 +67,80 @@
6767
<!-- 这里可写当前语言的特殊实现逻辑 -->
6868

6969
```python
70-
70+
# Definition for a binary tree node.
71+
# class TreeNode:
72+
# def __init__(self, val=0, left=None, right=None):
73+
# self.val = val
74+
# self.left = left
75+
# self.right = right
76+
class Solution:
77+
def pruneTree(self, root: TreeNode) -> TreeNode:
78+
if not root:
79+
return None
80+
root.left = self.pruneTree(root.left)
81+
root.right = self.pruneTree(root.right)
82+
if root.val == 0 and not root.left and not root.right:
83+
return None
84+
return root
7185
```
7286

7387
### **Java**
7488

7589
<!-- 这里可写当前语言的特殊实现逻辑 -->
7690

7791
```java
92+
/**
93+
* Definition for a binary tree node.
94+
* public class TreeNode {
95+
* int val;
96+
* TreeNode left;
97+
* TreeNode right;
98+
* TreeNode() {}
99+
* TreeNode(int val) { this.val = val; }
100+
* TreeNode(int val, TreeNode left, TreeNode right) {
101+
* this.val = val;
102+
* this.left = left;
103+
* this.right = right;
104+
* }
105+
* }
106+
*/
107+
class Solution {
108+
public TreeNode pruneTree(TreeNode root) {
109+
if (root == null) {
110+
return null;
111+
}
112+
root.left = pruneTree(root.left);
113+
root.right = pruneTree(root.right);
114+
if (root.val == 0 && root.left == null && root.right == null) {
115+
return null;
116+
}
117+
return root;
118+
}
119+
}
120+
```
78121

122+
### **Go**
123+
124+
```go
125+
/**
126+
* Definition for a binary tree node.
127+
* type TreeNode struct {
128+
* Val int
129+
* Left *TreeNode
130+
* Right *TreeNode
131+
* }
132+
*/
133+
func pruneTree(root *TreeNode) *TreeNode {
134+
if root == nil {
135+
return nil
136+
}
137+
root.Left = pruneTree(root.Left)
138+
root.Right = pruneTree(root.Right)
139+
if root.Val == 0 && root.Left == nil && root.Right == nil {
140+
return nil
141+
}
142+
return root
143+
}
79144
```
80145

81146
### **...**
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* type TreeNode struct {
4+
* Val int
5+
* Left *TreeNode
6+
* Right *TreeNode
7+
* }
8+
*/
9+
func pruneTree(root *TreeNode) *TreeNode {
10+
if root == nil {
11+
return nil
12+
}
13+
root.Left = pruneTree(root.Left)
14+
root.Right = pruneTree(root.Right)
15+
if root.Val == 0 && root.Left == nil && root.Right == nil {
16+
return nil
17+
}
18+
return root
19+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
if (root == null) {
19+
return null;
20+
}
21+
root.left = pruneTree(root.left);
22+
root.right = pruneTree(root.right);
23+
if (root.val == 0 && root.left == null && root.right == null) {
24+
return null;
25+
}
26+
return root;
27+
}
28+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def pruneTree(self, root: TreeNode) -> TreeNode:
9+
if not root:
10+
return None
11+
root.left = self.pruneTree(root.left)
12+
root.right = self.pruneTree(root.right)
13+
if root.val == 0 and not root.left and not root.right:
14+
return None
15+
return root

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

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
<strong>示例1:</strong>
1717
<strong>输入:</strong> [1,null,0,0,1]
1818
<strong>输出: </strong>[1,null,0,null,1]
19-
20-
<strong>解释:</strong>
19+
20+
<strong>解释:</strong>
2121
只有红色节点满足条件&ldquo;所有不包含 1 的子树&rdquo;
2222
右图为返回的答案。
2323

@@ -61,15 +61,80 @@
6161
<!-- 这里可写当前语言的特殊实现逻辑 -->
6262

6363
```python
64-
64+
# Definition for a binary tree node.
65+
# class TreeNode:
66+
# def __init__(self, val=0, left=None, right=None):
67+
# self.val = val
68+
# self.left = left
69+
# self.right = right
70+
class Solution:
71+
def pruneTree(self, root: TreeNode) -> TreeNode:
72+
if not root:
73+
return None
74+
root.left = self.pruneTree(root.left)
75+
root.right = self.pruneTree(root.right)
76+
if root.val == 0 and not root.left and not root.right:
77+
return None
78+
return root
6579
```
6680

6781
### **Java**
6882

6983
<!-- 这里可写当前语言的特殊实现逻辑 -->
7084

7185
```java
86+
/**
87+
* Definition for a binary tree node.
88+
* public class TreeNode {
89+
* int val;
90+
* TreeNode left;
91+
* TreeNode right;
92+
* TreeNode() {}
93+
* TreeNode(int val) { this.val = val; }
94+
* TreeNode(int val, TreeNode left, TreeNode right) {
95+
* this.val = val;
96+
* this.left = left;
97+
* this.right = right;
98+
* }
99+
* }
100+
*/
101+
class Solution {
102+
public TreeNode pruneTree(TreeNode root) {
103+
if (root == null) {
104+
return null;
105+
}
106+
root.left = pruneTree(root.left);
107+
root.right = pruneTree(root.right);
108+
if (root.val == 0 && root.left == null && root.right == null) {
109+
return null;
110+
}
111+
return root;
112+
}
113+
}
114+
```
72115

116+
### **Go**
117+
118+
```go
119+
/**
120+
* Definition for a binary tree node.
121+
* type TreeNode struct {
122+
* Val int
123+
* Left *TreeNode
124+
* Right *TreeNode
125+
* }
126+
*/
127+
func pruneTree(root *TreeNode) *TreeNode {
128+
if root == nil {
129+
return nil
130+
}
131+
root.Left = pruneTree(root.Left)
132+
root.Right = pruneTree(root.Right)
133+
if root.Val == 0 && root.Left == nil && root.Right == nil {
134+
return nil
135+
}
136+
return root
137+
}
73138
```
74139

75140
### **...**

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

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
<strong>Example 1:</strong>
1515
<strong>Input:</strong> [1,null,0,0,1]
1616
<strong>Output: </strong>[1,null,0,null,1]
17-
18-
<strong>Explanation:</strong>
17+
18+
<strong>Explanation:</strong>
1919
Only the red nodes satisfy the property &quot;every subtree not containing a 1&quot;.
2020
The diagram on the right represents the answer.
2121

@@ -55,13 +55,78 @@ The diagram on the right represents the answer.
5555
### **Python3**
5656

5757
```python
58-
58+
# Definition for a binary tree node.
59+
# class TreeNode:
60+
# def __init__(self, val=0, left=None, right=None):
61+
# self.val = val
62+
# self.left = left
63+
# self.right = right
64+
class Solution:
65+
def pruneTree(self, root: TreeNode) -> TreeNode:
66+
if not root:
67+
return None
68+
root.left = self.pruneTree(root.left)
69+
root.right = self.pruneTree(root.right)
70+
if root.val == 0 and not root.left and not root.right:
71+
return None
72+
return root
5973
```
6074

6175
### **Java**
6276

6377
```java
78+
/**
79+
* Definition for a binary tree node.
80+
* public class TreeNode {
81+
* int val;
82+
* TreeNode left;
83+
* TreeNode right;
84+
* TreeNode() {}
85+
* TreeNode(int val) { this.val = val; }
86+
* TreeNode(int val, TreeNode left, TreeNode right) {
87+
* this.val = val;
88+
* this.left = left;
89+
* this.right = right;
90+
* }
91+
* }
92+
*/
93+
class Solution {
94+
public TreeNode pruneTree(TreeNode root) {
95+
if (root == null) {
96+
return null;
97+
}
98+
root.left = pruneTree(root.left);
99+
root.right = pruneTree(root.right);
100+
if (root.val == 0 && root.left == null && root.right == null) {
101+
return null;
102+
}
103+
return root;
104+
}
105+
}
106+
```
64107

108+
### **Go**
109+
110+
```go
111+
/**
112+
* Definition for a binary tree node.
113+
* type TreeNode struct {
114+
* Val int
115+
* Left *TreeNode
116+
* Right *TreeNode
117+
* }
118+
*/
119+
func pruneTree(root *TreeNode) *TreeNode {
120+
if root == nil {
121+
return nil
122+
}
123+
root.Left = pruneTree(root.Left)
124+
root.Right = pruneTree(root.Right)
125+
if root.Val == 0 && root.Left == nil && root.Right == nil {
126+
return nil
127+
}
128+
return root
129+
}
65130
```
66131

67132
### **...**
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* type TreeNode struct {
4+
* Val int
5+
* Left *TreeNode
6+
* Right *TreeNode
7+
* }
8+
*/
9+
func pruneTree(root *TreeNode) *TreeNode {
10+
if root == nil {
11+
return nil
12+
}
13+
root.Left = pruneTree(root.Left)
14+
root.Right = pruneTree(root.Right)
15+
if root.Val == 0 && root.Left == nil && root.Right == nil {
16+
return nil
17+
}
18+
return root
19+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /