Skip to content

Commit ea52847

Browse files
committed
feat: add solutions to lc problem: No.1325
No.1325.Delete Leaves With a Given Value
1 parent 19a2192 commit ea52847

File tree

21 files changed

+214
-192
lines changed

21 files changed

+214
-192
lines changed

solution/0500-0599/0555.Split Concatenated Strings/Solution.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ def splitLoopedString(self, strs: List[str]) -> str:
33
strs = [s[::-1] if s[::-1] > s else s for s in strs]
44
ans = ''.join(strs)
55
for i, s in enumerate(strs):
6-
t = ''.join(strs[i + 1:]) + ''.join(strs[: i])
6+
t = ''.join(strs[i + 1 :]) + ''.join(strs[:i])
77
for j in range(len(s)):
88
a = s[j:]
99
b = s[:j]

solution/0800-0899/0831.Masking Personal Information/Solution.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ class Solution:
22
def maskPII(self, s: str) -> str:
33
if s[0].isalpha():
44
s = s.lower()
5-
return s[0] + '*****' + s[s.find('@') - 1:]
5+
return s[0] + '*****' + s[s.find('@') - 1 :]
66
s = ''.join(c for c in s if c.isdigit())
77
cnt = len(s) - 10
88
suf = '***-***-' + s[-4:]

solution/0800-0899/0833.Find And Replace in String/Solution.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
class Solution:
2-
def findReplaceString(self, s: str, indices: List[int], sources: List[str], targets: List[str]) -> str:
2+
def findReplaceString(
3+
self, s: str, indices: List[int], sources: List[str], targets: List[str]
4+
) -> str:
35
n = len(s)
46
d = [-1] * n
57
for i, (j, source) in enumerate(zip(indices, sources)):
6-
if s[j: j + len(source)] == source:
8+
if s[j : j + len(source)] == source:
79
d[j] = i
810
ans = []
911
i = 0

solution/1100-1199/1125.Smallest Sufficient Team/Solution.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
class Solution:
2-
def smallestSufficientTeam(self, req_skills: List[str], people: List[List[str]]) -> List[int]:
2+
def smallestSufficientTeam(
3+
self, req_skills: List[str], people: List[List[str]]
4+
) -> List[int]:
35
@cache
46
def dfs(i, state):
57
if i == n:

solution/1200-1299/1223.Dice Roll Simulation/Solution.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ def dfs(i, j, x):
1010
ans += dfs(i + 1, k, 1)
1111
elif x < rollMax[j - 1]:
1212
ans += dfs(i + 1, j, x + 1)
13-
return ans % (10 ** 9 + 7)
13+
return ans % (10**9 + 7)
1414

1515
return dfs(0, 0, 0)

solution/1200-1299/1229.Meeting Scheduler/Solution.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
class Solution:
2-
def minAvailableDuration(self, slots1: List[List[int]], slots2: List[List[int]], duration: int) -> List[int]:
2+
def minAvailableDuration(
3+
self, slots1: List[List[int]], slots2: List[List[int]], duration: int
4+
) -> List[int]:
35
slots1.sort()
46
slots2.sort()
57
m, n = len(slots1), len(slots2)

solution/1200-1299/1253.Reconstruct a 2-Row Binary Matrix/Solution.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
class Solution:
2-
def reconstructMatrix(self, upper: int, lower: int, colsum: List[int]) -> List[List[int]]:
2+
def reconstructMatrix(
3+
self, upper: int, lower: int, colsum: List[int]
4+
) -> List[List[int]]:
35
n = len(colsum)
46
ans = [[0] * n for _ in range(2)]
57
for j, v in enumerate(colsum):

solution/1200-1299/1255.Maximum Score Words Formed by Letters/Solution.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
class Solution:
2-
def maxScoreWords(self, words: List[str], letters: List[str], score: List[int]) -> int:
2+
def maxScoreWords(
3+
self, words: List[str], letters: List[str], score: List[int]
4+
) -> int:
35
cnt = Counter(letters)
46
n = len(words)
57
ans = 0

solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/Solution.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
# self.left = left
66
# self.right = right
77
class FindElements:
8-
98
def __init__(self, root: Optional[TreeNode]):
109
def dfs(root):
1110
self.vis.add(root.val)

solution/1300-1399/1325.Delete Leaves With a Given Value/README.md

Lines changed: 69 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,15 @@
6767

6868
<!-- 这里可写通用的实现逻辑 -->
6969

70-
后序遍历,遇到叶子节点值为 target 的时候,将叶子节点置为 null。此过程使用一个父节点来完成。
70+
**方法一:递归**
71+
72+
我们先判断 $root$ 节点是否为空,若为空,则返回空。
73+
74+
否则,递归地处理 $root$ 的左右子树,即调用 `root.left = removeLeafNodes(root.left, target)``root.right = removeLeafNodes(root.right, target)`
75+
76+
然后判断 $root$ 节点是否为叶子节点,即判断 $root.left$ 和 $root.right$ 是否为空,且 $root.val$ 是否等于 $target$。若是,则返回空,否则返回 $root$。
77+
78+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为二叉树的节点个数。
7179

7280
<!-- tabs:start -->
7381

@@ -86,20 +94,13 @@ class Solution:
8694
def removeLeafNodes(
8795
self, root: Optional[TreeNode], target: int
8896
) -> Optional[TreeNode]:
89-
def dfs(root, prev):
90-
if root is None:
91-
return
92-
dfs(root.left, root)
93-
dfs(root.right, root)
94-
if root.left is None and root.right is None and root.val == target:
95-
if prev.left == root:
96-
prev.left = None
97-
else:
98-
prev.right = None
99-
100-
p = TreeNode(val=0, left=root)
101-
dfs(root, p)
102-
return p.left
97+
if root is None:
98+
return None
99+
root.left = self.removeLeafNodes(root.left, target)
100+
root.right = self.removeLeafNodes(root.right, target)
101+
if root.left is None and root.right is None and root.val == target:
102+
return None
103+
return root
103104
```
104105

105106
### **Java**
@@ -124,24 +125,15 @@ class Solution:
124125
*/
125126
class Solution {
126127
public TreeNode removeLeafNodes(TreeNode root, int target) {
127-
TreeNode p = new TreeNode(0, root, null);
128-
dfs(root, p, target);
129-
return p.left;
130-
}
131-
132-
private void dfs(TreeNode root, TreeNode prev, int target) {
133128
if (root == null) {
134-
return;
129+
return null;
135130
}
136-
dfs(root.left, root, target);
137-
dfs(root.right, root, target);
131+
root.left = removeLeafNodes(root.left, target);
132+
root.right = removeLeafNodes(root.right, target);
138133
if (root.left == null && root.right == null && root.val == target) {
139-
if (prev.left == root) {
140-
prev.left = null;
141-
} else {
142-
prev.right = null;
143-
}
134+
return null;
144135
}
136+
return root;
145137
}
146138
}
147139
```
@@ -163,21 +155,15 @@ class Solution {
163155
class Solution {
164156
public:
165157
TreeNode* removeLeafNodes(TreeNode* root, int target) {
166-
TreeNode* p = new TreeNode(0, root, nullptr);
167-
dfs(root, p, target);
168-
return p->left;
169-
}
170-
171-
void dfs(TreeNode* root, TreeNode* prev, int target) {
172-
if (!root) return;
173-
dfs(root->left, root, target);
174-
dfs(root->right, root, target);
158+
if (!root) {
159+
return nullptr;
160+
}
161+
root->left = removeLeafNodes(root->left, target);
162+
root->right = removeLeafNodes(root->right, target);
175163
if (!root->left && !root->right && root->val == target) {
176-
if (prev->left == root)
177-
prev->left = nullptr;
178-
else
179-
prev->right = nullptr;
164+
return nullptr;
180165
}
166+
return root;
181167
}
182168
};
183169
```
@@ -194,24 +180,48 @@ public:
194180
* }
195181
*/
196182
func removeLeafNodes(root *TreeNode, target int) *TreeNode {
197-
p := &TreeNode{0, root, nil}
198-
var dfs func(root, prev *TreeNode)
199-
dfs = func(root, prev *TreeNode) {
200-
if root == nil {
201-
return
202-
}
203-
dfs(root.Left, root)
204-
dfs(root.Right, root)
205-
if root.Left == nil && root.Right == nil && root.Val == target {
206-
if prev.Left == root {
207-
prev.Left = nil
208-
} else {
209-
prev.Right = nil
210-
}
211-
}
183+
if root == nil {
184+
return nil
212185
}
213-
dfs(root, p)
214-
return p.Left
186+
root.Left = removeLeafNodes(root.Left, target)
187+
root.Right = removeLeafNodes(root.Right, target)
188+
if root.Left == nil && root.Right == nil && root.Val == target {
189+
return nil
190+
}
191+
return root
192+
}
193+
```
194+
195+
### **TypeScript**
196+
197+
```ts
198+
/**
199+
* Definition for a binary tree node.
200+
* class TreeNode {
201+
* val: number
202+
* left: TreeNode | null
203+
* right: TreeNode | null
204+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
205+
* this.val = (val===undefined ? 0 : val)
206+
* this.left = (left===undefined ? null : left)
207+
* this.right = (right===undefined ? null : right)
208+
* }
209+
* }
210+
*/
211+
212+
function removeLeafNodes(
213+
root: TreeNode | null,
214+
target: number,
215+
): TreeNode | null {
216+
if (!root) {
217+
return null;
218+
}
219+
root.left = removeLeafNodes(root.left, target);
220+
root.right = removeLeafNodes(root.right, target);
221+
if (!root.left && !root.right && root.val == target) {
222+
return null;
223+
}
224+
return root;
215225
}
216226
```
217227

0 commit comments

Comments
 (0)