Skip to content

Commit ab76747

Browse files
committed
feat: add solutions to lc problem: No.1080
No.1080.Insufficient Nodes in Root to Leaf Paths
1 parent 5392e8c commit ab76747

File tree

5 files changed

+205
-0
lines changed

5 files changed

+205
-0
lines changed

solution/1000-1099/1080.Insufficient Nodes in Root to Leaf Paths/README.md

+77
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,99 @@
5151

5252
<!-- 这里可写通用的实现逻辑 -->
5353

54+
递归遍历整棵树,如果到达叶子结点且路径和小于 limit,直接返回 null 表示删除。如果左右子树都被删除,说明经过当前结点的路径和也一定小于 limit,同样需要删除
55+
5456
<!-- tabs:start -->
5557

5658
### **Python3**
5759

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

6062
```python
63+
class Solution:
64+
def sufficientSubset(self, root: TreeNode, limit: int) -> TreeNode:
65+
if root is None:
66+
return None
67+
68+
limit -= root.val
69+
if root.left is None and root.right is None:
70+
return None if limit > 0 else root
71+
72+
root.left = self.sufficientSubset(root.left, limit)
73+
root.right = self.sufficientSubset(root.right, limit)
6174

75+
if root.left is None and root.right is None:
76+
return None
77+
return root
6278
```
6379

6480
### **Java**
6581

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

6884
```java
85+
class Solution {
86+
public TreeNode sufficientSubset(TreeNode root, int limit) {
87+
if (root == null) {
88+
return null;
89+
}
90+
limit -= root.val;
91+
if (root.left == null && root.right == null) {
92+
return limit > 0 ? null : root;
93+
}
94+
root.left = sufficientSubset(root.left, limit);
95+
root.right = sufficientSubset(root.right, limit);
96+
return root.left == null && root.right == null ? null : root;
97+
}
98+
}
99+
```
100+
101+
### **Go**
102+
103+
```go
104+
func sufficientSubset(root *TreeNode, limit int) *TreeNode {
105+
if root == nil {
106+
return nil
107+
}
108+
109+
limit -= root.Val
110+
if root.Left == nil && root.Right == nil {
111+
if limit > 0 {
112+
return nil
113+
}
114+
return root
115+
}
116+
117+
root.Left = sufficientSubset(root.Left, limit)
118+
root.Right = sufficientSubset(root.Right, limit)
119+
120+
if root.Left == nil && root.Right == nil {
121+
return nil
122+
}
123+
return root
124+
}
125+
```
126+
127+
### **C++**
128+
129+
```cpp
130+
class Solution {
131+
public:
132+
TreeNode* sufficientSubset(TreeNode* root, int limit) {
133+
if (root == nullptr) return nullptr;
134+
135+
limit -= root->val;
136+
if (root->left == nullptr && root->right == nullptr)
137+
return limit > 0 ? nullptr : root;
138+
139+
root->left = sufficientSubset(root->left, limit);
140+
root->right = sufficientSubset(root->right, limit);
69141

142+
if (root->left == nullptr && root->right == nullptr)
143+
return nullptr;
144+
return root;
145+
}
146+
};
70147
```
71148

72149
### **...**

solution/1000-1099/1080.Insufficient Nodes in Root to Leaf Paths/README_EN.md

+75
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,88 @@
4848
### **Python3**
4949

5050
```python
51+
class Solution:
52+
def sufficientSubset(self, root: TreeNode, limit: int) -> TreeNode:
53+
if root is None:
54+
return None
5155

56+
limit -= root.val
57+
if root.left is None and root.right is None:
58+
return None if limit > 0 else root
59+
60+
root.left = self.sufficientSubset(root.left, limit)
61+
root.right = self.sufficientSubset(root.right, limit)
62+
63+
if root.left is None and root.right is None:
64+
return None
65+
return root
5266
```
5367

5468
### **Java**
5569

5670
```java
71+
class Solution {
72+
public TreeNode sufficientSubset(TreeNode root, int limit) {
73+
if (root == null) {
74+
return null;
75+
}
76+
limit -= root.val;
77+
if (root.left == null && root.right == null) {
78+
return limit > 0 ? null : root;
79+
}
80+
root.left = sufficientSubset(root.left, limit);
81+
root.right = sufficientSubset(root.right, limit);
82+
return root.left == null && root.right == null ? null : root;
83+
}
84+
}
85+
```
86+
87+
### **Go**
88+
89+
```go
90+
func sufficientSubset(root *TreeNode, limit int) *TreeNode {
91+
if root == nil {
92+
return nil
93+
}
94+
95+
limit -= root.Val
96+
if root.Left == nil && root.Right == nil {
97+
if limit > 0 {
98+
return nil
99+
}
100+
return root
101+
}
102+
103+
root.Left = sufficientSubset(root.Left, limit)
104+
root.Right = sufficientSubset(root.Right, limit)
105+
106+
if root.Left == nil && root.Right == nil {
107+
return nil
108+
}
109+
return root
110+
}
111+
```
112+
113+
### **C++**
114+
115+
```cpp
116+
class Solution {
117+
public:
118+
TreeNode* sufficientSubset(TreeNode* root, int limit) {
119+
if (root == nullptr) return nullptr;
120+
121+
limit -= root->val;
122+
if (root->left == nullptr && root->right == nullptr)
123+
return limit > 0 ? nullptr : root;
124+
125+
root->left = sufficientSubset(root->left, limit);
126+
root->right = sufficientSubset(root->right, limit);
57127

128+
if (root->left == nullptr && root->right == nullptr)
129+
return nullptr;
130+
return root;
131+
}
132+
};
58133
```
59134

60135
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
func sufficientSubset(root *TreeNode, limit int) *TreeNode {
2+
if root == nil {
3+
return nil
4+
}
5+
6+
limit -= root.Val
7+
if root.Left == nil && root.Right == nil {
8+
if limit > 0 {
9+
return nil
10+
}
11+
return root
12+
}
13+
14+
root.Left = sufficientSubset(root.Left, limit)
15+
root.Right = sufficientSubset(root.Right, limit)
16+
17+
if root.Left == nil && root.Right == nil {
18+
return nil
19+
}
20+
return root
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
TreeNode* sufficientSubset(TreeNode* root, int limit) {
4+
if (root == nullptr) return nullptr;
5+
6+
limit -= root->val;
7+
if (root->left == nullptr && root->right == nullptr)
8+
return limit > 0 ? nullptr : root;
9+
10+
root->left = sufficientSubset(root->left, limit);
11+
root->right = sufficientSubset(root->right, limit);
12+
13+
if (root->left == nullptr && root->right == nullptr)
14+
return nullptr;
15+
return root;
16+
}
17+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def sufficientSubset(self, root: TreeNode, limit: int) -> TreeNode:
3+
if root is None:
4+
return None
5+
6+
limit -= root.val
7+
if root.left is None and root.right is None:
8+
return None if limit > 0 else root
9+
10+
root.left = self.sufficientSubset(root.left, limit)
11+
root.right = self.sufficientSubset(root.right, limit)
12+
13+
if root.left is None and root.right is None:
14+
return None
15+
return root

0 commit comments

Comments
 (0)