Skip to content

Commit 07d4b84

Browse files
committed
feat: add solutions to lc problem: No.1302
No.1302.Deepest Leaves Sum
1 parent 69b99f2 commit 07d4b84

File tree

6 files changed

+394
-145
lines changed

6 files changed

+394
-145
lines changed

solution/1300-1399/1302.Deepest Leaves Sum/README.md

Lines changed: 178 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,12 @@
4343

4444
可以忽略一些细节,每次都统计当前遍历层级的数值和,当 BFS 结束时,最后一次数值和便是结果。
4545

46+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是树中节点的数目。
47+
4648
**方法二:DFS**
4749

50+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是树中节点的数目。
51+
4852
<!-- tabs:start -->
4953

5054
### **Python3**
@@ -59,20 +63,44 @@
5963
# self.left = left
6064
# self.right = right
6165
class Solution:
62-
def deepestLeavesSum(self, root: TreeNode) -> int:
66+
def deepestLeavesSum(self, root: Optional[TreeNode]) -> int:
6367
q = deque([root])
64-
s = 0
6568
while q:
66-
n = len(q)
67-
s = 0
68-
for _ in range(n):
69-
node = q.popleft()
70-
s += node.val
71-
if node.left:
72-
q.append(node.left)
73-
if node.right:
74-
q.append(node.right)
75-
return s
69+
ans = 0
70+
for _ in range(len(q)):
71+
root = q.popleft()
72+
ans += root.val
73+
if root.left:
74+
q.append(root.left)
75+
if root.right:
76+
q.append(root.right)
77+
return ans
78+
```
79+
80+
```python
81+
# Definition for a binary tree node.
82+
# class TreeNode:
83+
# def __init__(self, val=0, left=None, right=None):
84+
# self.val = val
85+
# self.left = left
86+
# self.right = right
87+
class Solution:
88+
def deepestLeavesSum(self, root: Optional[TreeNode]) -> int:
89+
def dfs(root, i):
90+
nonlocal ans, mx
91+
if root is None:
92+
return
93+
if i == mx:
94+
ans += root.val
95+
elif i > mx:
96+
ans = root.val
97+
mx = i
98+
dfs(root.left, i + 1)
99+
dfs(root.right, i + 1)
100+
101+
ans = mx = 0
102+
dfs(root, 1)
103+
return ans
76104
```
77105

78106
### **Java**
@@ -99,22 +127,62 @@ class Solution {
99127
public int deepestLeavesSum(TreeNode root) {
100128
Deque<TreeNode> q = new ArrayDeque<>();
101129
q.offer(root);
102-
int s = 0;
130+
int ans = 0;
103131
while (!q.isEmpty()) {
104-
int n = q.size();
105-
s = 0;
106-
while (n-- > 0) {
107-
TreeNode node = q.poll();
108-
s += node.val;
109-
if (node.left != null) {
110-
q.offer(node.left);
132+
ans = 0;
133+
for (int n = q.size(); n > 0; --n) {
134+
root = q.pollFirst();
135+
ans += root.val;
136+
if (root.left != null) {
137+
q.offer(root.left);
111138
}
112-
if (node.right != null) {
113-
q.offer(node.right);
139+
if (root.right != null) {
140+
q.offer(root.right);
114141
}
115142
}
116143
}
117-
return s;
144+
return ans;
145+
}
146+
}
147+
```
148+
149+
```java
150+
/**
151+
* Definition for a binary tree node.
152+
* public class TreeNode {
153+
* int val;
154+
* TreeNode left;
155+
* TreeNode right;
156+
* TreeNode() {}
157+
* TreeNode(int val) { this.val = val; }
158+
* TreeNode(int val, TreeNode left, TreeNode right) {
159+
* this.val = val;
160+
* this.left = left;
161+
* this.right = right;
162+
* }
163+
* }
164+
*/
165+
class Solution {
166+
int mx;
167+
int ans;
168+
169+
public int deepestLeavesSum(TreeNode root) {
170+
dfs(root, 1);
171+
return ans;
172+
}
173+
174+
private void dfs(TreeNode root, int i) {
175+
if (root == null) {
176+
return;
177+
}
178+
if (i > mx) {
179+
mx = i;
180+
ans = root.val;
181+
} else if (i == mx) {
182+
ans += root.val;
183+
}
184+
dfs(root.left, i + 1);
185+
dfs(root.right, i + 1);
118186
}
119187
}
120188
```
@@ -136,21 +204,55 @@ class Solution {
136204
class Solution {
137205
public:
138206
int deepestLeavesSum(TreeNode* root) {
139-
queue<TreeNode*> q;
140-
q.push(root);
141-
int s = 0;
207+
int ans = 0;
208+
queue<TreeNode*> q {{root}};
142209
while (!q.empty()) {
143-
int n = q.size();
144-
s = 0;
145-
while (n--) {
146-
auto node = q.front();
210+
ans = 0;
211+
for (int n = q.size(); n; --n) {
212+
root = q.front();
147213
q.pop();
148-
s += node->val;
149-
if (node->left) q.push(node->left);
150-
if (node->right) q.push(node->right);
214+
ans += root->val;
215+
if (root->left) q.push(root->left);
216+
if (root->right) q.push(root->right);
151217
}
152218
}
153-
return s;
219+
return ans;
220+
}
221+
};
222+
```
223+
224+
```cpp
225+
/**
226+
* Definition for a binary tree node.
227+
* struct TreeNode {
228+
* int val;
229+
* TreeNode *left;
230+
* TreeNode *right;
231+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
232+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
233+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
234+
* };
235+
*/
236+
class Solution {
237+
public:
238+
int mx = 0;
239+
int ans = 0;
240+
241+
int deepestLeavesSum(TreeNode* root) {
242+
dfs(root, 1);
243+
return ans;
244+
}
245+
246+
void dfs(TreeNode* root, int i) {
247+
if (!root) return;
248+
if (i == mx) {
249+
ans += root->val;
250+
} else if (i > mx) {
251+
mx = i;
252+
ans = root->val;
253+
}
254+
dfs(root->left, i + 1);
255+
dfs(root->right, i + 1);
154256
}
155257
};
156258
```
@@ -167,25 +269,53 @@ public:
167269
* }
168270
*/
169271
func deepestLeavesSum(root *TreeNode) int {
170-
q := []*TreeNode{}
171-
q = append(q, root)
172-
s := 0
173-
for len(q) != 0 {
174-
n := len(q)
175-
s = 0
176-
for i := 0; i < n; i++ {
177-
node := q[0]
272+
q := []*TreeNode{root}
273+
ans := 0
274+
for len(q) > 0 {
275+
ans = 0
276+
for n := len(q); n > 0; n-- {
277+
root = q[0]
178278
q = q[1:]
179-
s += node.Val
180-
if node.Left != nil {
181-
q = append(q, node.Left)
279+
ans += root.Val
280+
if root.Left != nil {
281+
q = append(q, root.Left)
182282
}
183-
if node.Right != nil {
184-
q = append(q, node.Right)
283+
if root.Right != nil {
284+
q = append(q, root.Right)
185285
}
186286
}
187287
}
188-
return s
288+
return ans
289+
}
290+
```
291+
292+
```go
293+
/**
294+
* Definition for a binary tree node.
295+
* type TreeNode struct {
296+
* Val int
297+
* Left *TreeNode
298+
* Right *TreeNode
299+
* }
300+
*/
301+
func deepestLeavesSum(root *TreeNode) int {
302+
ans, mx := 0, 0
303+
var dfs func(*TreeNode, int)
304+
dfs = func(root *TreeNode, i int) {
305+
if root == nil {
306+
return
307+
}
308+
if i == mx {
309+
ans += root.Val
310+
} else if i > mx {
311+
mx = i
312+
ans = root.Val
313+
}
314+
dfs(root.Left, i+1)
315+
dfs(root.Right, i+1)
316+
}
317+
dfs(root, 1)
318+
return ans
189319
}
190320
```
191321

0 commit comments

Comments
 (0)