Skip to content

Commit 999a643

Browse files
committed
feat: add solutions to lc/lcof2 problem:Sum Root to Leaf Numbers
1 parent a7390b3 commit 999a643

File tree

27 files changed

+537
-96
lines changed

27 files changed

+537
-96
lines changed

lcof2/剑指 Offer II 049. 从根节点到叶节点的路径数字之和/README.md

+105-7
Original file line numberDiff line numberDiff line change
@@ -63,28 +63,126 @@
6363

6464
<!-- 这里可写通用的实现逻辑 -->
6565

66+
DFS。
67+
6668
<!-- tabs:start -->
6769

6870
### **Python3**
6971

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

7274
```python
73-
75+
# Definition for a binary tree node.
76+
# class TreeNode:
77+
# def __init__(self, val=0, left=None, right=None):
78+
# self.val = val
79+
# self.left = left
80+
# self.right = right
81+
class Solution:
82+
def sumNumbers(self, root: TreeNode) -> int:
83+
def dfs(root, presum):
84+
if root is None:
85+
return 0
86+
s = 10 * presum + root.val
87+
if root.left is None and root.right is None:
88+
return s
89+
return dfs(root.left, s) + dfs(root.right, s)
90+
91+
return dfs(root, 0)
7492
```
7593

7694
### **Java**
7795

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

8098
```java
81-
82-
```
83-
84-
### **...**
85-
99+
/**
100+
* Definition for a binary tree node.
101+
* public class TreeNode {
102+
* int val;
103+
* TreeNode left;
104+
* TreeNode right;
105+
* TreeNode() {}
106+
* TreeNode(int val) { this.val = val; }
107+
* TreeNode(int val, TreeNode left, TreeNode right) {
108+
* this.val = val;
109+
* this.left = left;
110+
* this.right = right;
111+
* }
112+
* }
113+
*/
114+
class Solution {
115+
public int sumNumbers(TreeNode root) {
116+
return dfs(root, 0);
117+
}
118+
119+
private int dfs(TreeNode root, int presum) {
120+
if (root == null) {
121+
return 0;
122+
}
123+
int s = presum * 10 + root.val;
124+
if (root.left == null && root.right == null) {
125+
return s;
126+
}
127+
return dfs(root.left, s) + dfs(root.right, s);
128+
}
129+
}
86130
```
87131

132+
### **C++**
133+
134+
```cpp
135+
/**
136+
* Definition for a binary tree node.
137+
* struct TreeNode {
138+
* int val;
139+
* TreeNode *left;
140+
* TreeNode *right;
141+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
142+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
143+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
144+
* };
145+
*/
146+
class Solution {
147+
public:
148+
int sumNumbers(TreeNode *root) {
149+
return dfs(root, 0);
150+
}
151+
152+
int dfs(TreeNode *root, int presum) {
153+
if (root == nullptr)
154+
return 0;
155+
int s = presum * 10 + root->val;
156+
if (root->left == nullptr && root->right == nullptr)
157+
return s;
158+
return dfs(root->left, s) + dfs(root->right, s);
159+
}
160+
};
88161
```
89162

90-
<!-- tabs:end -->
163+
### **Go**
164+
165+
```go
166+
/**
167+
* Definition for a binary tree node.
168+
* type TreeNode struct {
169+
* Val int
170+
* Left *TreeNode
171+
* Right *TreeNode
172+
* }
173+
*/
174+
func sumNumbers(root *TreeNode) int {
175+
return dfs(root, 0)
176+
}
177+
178+
func dfs(root *TreeNode, presum int) int {
179+
if root == nil {
180+
return 0
181+
}
182+
s := presum*10 + root.Val
183+
if root.Left == nil && root.Right == nil {
184+
return s
185+
}
186+
return dfs(root.Left, s) + dfs(root.Right, s)
187+
}
188+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
int sumNumbers(TreeNode *root) {
15+
return dfs(root, 0);
16+
}
17+
18+
int dfs(TreeNode *root, int presum) {
19+
if (root == nullptr)
20+
return 0;
21+
int s = presum * 10 + root->val;
22+
if (root->left == nullptr && root->right == nullptr)
23+
return s;
24+
return dfs(root->left, s) + dfs(root->right, s);
25+
}
26+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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 sumNumbers(root *TreeNode) int {
10+
return dfs(root, 0)
11+
}
12+
13+
func dfs(root *TreeNode, presum int) int {
14+
if root == nil {
15+
return 0
16+
}
17+
s := presum*10 + root.Val
18+
if root.Left == nil && root.Right == nil {
19+
return s
20+
}
21+
return dfs(root.Left, s) + dfs(root.Right, s)
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 int sumNumbers(TreeNode root) {
18+
return dfs(root, 0);
19+
}
20+
21+
private int dfs(TreeNode root, int presum) {
22+
if (root == null) {
23+
return 0;
24+
}
25+
int s = presum * 10 + root.val;
26+
if (root.left == null && root.right == null) {
27+
return s;
28+
}
29+
return dfs(root.left, s) + dfs(root.right, s);
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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 sumNumbers(self, root: TreeNode) -> int:
9+
def dfs(root, presum):
10+
if root is None:
11+
return 0
12+
s = 10 * presum + root.val
13+
if root.left is None and root.right is None:
14+
return s
15+
return dfs(root.left, s) + dfs(root.right, s)
16+
17+
return dfs(root, 0)

lcof2/剑指 Offer II 105. 岛屿的最大面积/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ DFS 或并查集实现。
5353
模板 1——朴素并查集:
5454

5555
```python
56-
# 初始化,p存储每个点的祖宗节点
56+
# 初始化,p存储每个点的父节点
5757
p = list(range(n))
5858

5959
# 返回x的祖宗节点
@@ -70,7 +70,7 @@ p[find(a)] = find(b)
7070
模板 2——维护 size 的并查集:
7171

7272
```python
73-
# 初始化,p存储每个点的祖宗节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量
73+
# 初始化,p存储每个点的父节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量
7474
p = list(range(n))
7575
size = [1] * n
7676

@@ -90,7 +90,7 @@ if find(a) != find(b):
9090
模板 3——维护到祖宗节点距离的并查集:
9191

9292
```python
93-
# 初始化,p存储每个点的祖宗节点,d[x]存储x到p[x]的距离
93+
# 初始化,p存储每个点的父节点,d[x]存储x到p[x]的距离
9494
p = list(range(n))
9595
d = [0] * n
9696

lcof2/剑指 Offer II 116. 朋友圈/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
模板 1——朴素并查集:
6161

6262
```python
63-
# 初始化,p存储每个点的祖宗节点
63+
# 初始化,p存储每个点的父节点
6464
p = list(range(n))
6565

6666
# 返回x的祖宗节点
@@ -77,7 +77,7 @@ p[find(a)] = find(b)
7777
模板 2——维护 size 的并查集:
7878

7979
```python
80-
# 初始化,p存储每个点的祖宗节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量
80+
# 初始化,p存储每个点的父节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量
8181
p = list(range(n))
8282
size = [1] * n
8383

@@ -97,7 +97,7 @@ if find(a) != find(b):
9797
模板 3——维护到祖宗节点距离的并查集:
9898

9999
```python
100-
# 初始化,p存储每个点的祖宗节点,d[x]存储x到p[x]的距离
100+
# 初始化,p存储每个点的父节点,d[x]存储x到p[x]的距离
101101
p = list(range(n))
102102
d = [0] * n
103103

lcof2/剑指 Offer II 118. 多余的边/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
模板 1——朴素并查集:
5858

5959
```python
60-
# 初始化,p存储每个点的祖宗节点
60+
# 初始化,p存储每个点的父节点
6161
p = list(range(n))
6262

6363
# 返回x的祖宗节点
@@ -74,7 +74,7 @@ p[find(a)] = find(b)
7474
模板 2——维护 size 的并查集:
7575

7676
```python
77-
# 初始化,p存储每个点的祖宗节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量
77+
# 初始化,p存储每个点的父节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量
7878
p = list(range(n))
7979
size = [1] * n
8080

@@ -94,7 +94,7 @@ if find(a) != find(b):
9494
模板 3——维护到祖宗节点距离的并查集:
9595

9696
```python
97-
# 初始化,p存储每个点的祖宗节点,d[x]存储x到p[x]的距离
97+
# 初始化,p存储每个点的父节点,d[x]存储x到p[x]的距离
9898
p = list(range(n))
9999
d = [0] * n
100100

0 commit comments

Comments
 (0)