Skip to content

Commit de6b5df

Browse files
committed
feat: add solutions to lc problem: No.1022
No.1022.Sum of Root To Leaf Binary Numbers
1 parent fd65b5e commit de6b5df

File tree

13 files changed

+305
-18
lines changed

13 files changed

+305
-18
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -259,13 +259,13 @@
259259

260260
## 贡献者
261261

262-
非常感谢以下所有朋友对本项目的贡献,你们是最可爱的人
262+
感谢以下所有朋友对本项目的贡献
263263

264264
<a href="https://github.com/doocs/leetcode/graphs/contributors" target="_blank"><img src="./images/contributors.svg" /></a>
265265

266266
## 赞助者
267267

268-
特别感谢以下个人、组织对本项目的赞助!
268+
感谢以下个人、组织对本项目的赞助!
269269

270270
<a href="https://opencollective.com/doocs-leetcode/backers.svg?width=890" target="_blank"><img src="https://opencollective.com/doocs-leetcode/backers.svg?width=890"></a>
271271

lcci/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
## 题解
88

9-
以下所有题目均来源 [LeetCode 中国官网](https://leetcode-cn.com/problemset/lcci/),题解由 [doocs/leetcode 贡献者](https://github.com/doocs/leetcode/graphs/contributors) 提供,正在完善中,欢迎贡献你的题解!
9+
列表所有题解均由 [开源社区 Doocs](https://github.com/doocs) 贡献者提供,正在完善中,欢迎贡献你的题解!
1010

1111
快速搜索题号、题解、标签等,请善用 <kbd>Control</kbd>+<kbd>F</kbd>(或者 <kbd>Command</kbd>+<kbd>F</kbd>)。
1212

lcof/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
## 题解
1010

11-
以下所有题目均来源 [LeetCode 中国官网](https://leetcode-cn.com/problemset/lcof/),题解由 [doocs/leetcode 贡献者](https://github.com/doocs/leetcode/graphs/contributors) 提供,正在完善中,欢迎贡献你的题解!
11+
列表所有题解均由 [开源社区 Doocs](https://github.com/doocs) 贡献者提供,正在完善中,欢迎贡献你的题解!
1212

1313
快速搜索题号、题解、标签等,请善用 <kbd>Control</kbd>+<kbd>F</kbd>(或者 <kbd>Command</kbd>+<kbd>F</kbd>)。
1414

lcof2/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
## 题解
88

9-
以下所有题目均来源 [LeetCode 中国官网](https://leetcode-cn.com/problem-list/e8X3pBZi),题解由 [doocs/leetcode 贡献者](https://github.com/doocs/leetcode/graphs/contributors) 提供,正在完善中,欢迎贡献你的题解!
9+
列表所有题解均由 [开源社区 Doocs](https://github.com/doocs) 贡献者提供,正在完善中,欢迎贡献你的题解!
1010

1111
快速搜索题号、题解、标签等,请善用 <kbd>Control</kbd>+<kbd>F</kbd>(或者 <kbd>Command</kbd>+<kbd>F</kbd>)。
1212

lcp/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## 题解
44

5-
以下所有题目均来源 [LeetCode 中国官网](https://leetcode-cn.com),题解由 [doocs/leetcode 贡献者](https://github.com/doocs/leetcode/graphs/contributors) 提供,正在完善中,欢迎贡献你的题解!
5+
列表所有题解均由 [开源社区 Doocs](https://github.com/doocs) 贡献者提供,正在完善中,欢迎贡献你的题解!
66

77
快速搜索题号、题解、标签等,请善用 <kbd>Control</kbd>+<kbd>F</kbd>(或者 <kbd>Command</kbd>+<kbd>F</kbd>)。
88

lcs/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## 题解
44

5-
以下所有题目均来源 [LeetCode 中国官网](https://leetcode-cn.com),题解由 [doocs/leetcode 贡献者](https://github.com/doocs/leetcode/graphs/contributors) 提供,正在完善中,欢迎贡献你的题解!
5+
列表所有题解均由 [开源社区 Doocs](https://github.com/doocs) 贡献者提供,正在完善中,欢迎贡献你的题解!
66

77
快速搜索题号、题解、标签等,请善用 <kbd>Control</kbd>+<kbd>F</kbd>(或者 <kbd>Command</kbd>+<kbd>F</kbd>)。
88

solution/1000-1099/1022.Sum of Root To Leaf Binary Numbers/README.md

+106-2
Original file line numberDiff line numberDiff line change
@@ -52,27 +52,131 @@
5252
<li><code>Node.val</code> 为 <code>0</code> 或 <code>1</code> 。</li>
5353
</ul>
5454

55-
5655
## 解法
5756

5857
<!-- 这里可写通用的实现逻辑 -->
5958

59+
深度优先搜索 DFS 实现。
60+
6061
<!-- tabs:start -->
6162

6263
### **Python3**
6364

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

6667
```python
67-
68+
# Definition for a binary tree node.
69+
# class TreeNode:
70+
# def __init__(self, val=0, left=None, right=None):
71+
# self.val = val
72+
# self.left = left
73+
# self.right = right
74+
class Solution:
75+
def sumRootToLeaf(self, root: TreeNode) -> int:
76+
def dfs(root, t):
77+
if root is None:
78+
return 0
79+
t = (t << 1) | root.val
80+
if root.left is None and root.right is None:
81+
return t
82+
return dfs(root.left, t) + dfs(root.right, t)
83+
84+
return dfs(root, 0)
6885
```
6986

7087
### **Java**
7188

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

7491
```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 int sumRootToLeaf(TreeNode root) {
109+
return dfs(root, 0);
110+
}
111+
112+
private int dfs(TreeNode root, int t) {
113+
if (root == null) {
114+
return 0;
115+
}
116+
t = (t << 1) | root.val;
117+
if (root.left == null && root.right == null) {
118+
return t;
119+
}
120+
return dfs(root.left, t) + dfs(root.right, t);
121+
}
122+
}
123+
```
124+
125+
### **C++**
126+
127+
```cpp
128+
/**
129+
* Definition for a binary tree node.
130+
* struct TreeNode {
131+
* int val;
132+
* TreeNode *left;
133+
* TreeNode *right;
134+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
135+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
136+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
137+
* };
138+
*/
139+
class Solution {
140+
public:
141+
int sumRootToLeaf(TreeNode* root) {
142+
return dfs(root, 0);
143+
}
144+
145+
int dfs(TreeNode* root, int t) {
146+
if (!root) return 0;
147+
t = (t << 1) | root->val;
148+
if (!root->left && !root->right) return t;
149+
return dfs(root->left, t) + dfs(root->right, t);
150+
}
151+
};
152+
```
75153

154+
### **Go**
155+
156+
```go
157+
/**
158+
* Definition for a binary tree node.
159+
* type TreeNode struct {
160+
* Val int
161+
* Left *TreeNode
162+
* Right *TreeNode
163+
* }
164+
*/
165+
func sumRootToLeaf(root *TreeNode) int {
166+
var dfs func(root *TreeNode, t int) int
167+
dfs = func(root *TreeNode, t int) int {
168+
if root == nil {
169+
return 0
170+
}
171+
t = (t << 1) | root.Val
172+
if root.Left == nil && root.Right == nil {
173+
return t
174+
}
175+
return dfs(root.Left, t) + dfs(root.Right, t)
176+
}
177+
178+
return dfs(root, 0)
179+
}
76180
```
77181

78182
### **...**

solution/1000-1099/1022.Sum of Root To Leaf Binary Numbers/README_EN.md

+106-2
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,125 @@
4848
<li><code>Node.val</code> is <code>0</code> or <code>1</code>.</li>
4949
</ul>
5050

51-
5251
## Solutions
5352

53+
DFS.
54+
5455
<!-- tabs:start -->
5556

5657
### **Python3**
5758

5859
```python
59-
60+
# Definition for a binary tree node.
61+
# class TreeNode:
62+
# def __init__(self, val=0, left=None, right=None):
63+
# self.val = val
64+
# self.left = left
65+
# self.right = right
66+
class Solution:
67+
def sumRootToLeaf(self, root: TreeNode) -> int:
68+
def dfs(root, t):
69+
if root is None:
70+
return 0
71+
t = (t << 1) | root.val
72+
if root.left is None and root.right is None:
73+
return t
74+
return dfs(root.left, t) + dfs(root.right, t)
75+
76+
return dfs(root, 0)
6077
```
6178

6279
### **Java**
6380

6481
```java
82+
/**
83+
* Definition for a binary tree node.
84+
* public class TreeNode {
85+
* int val;
86+
* TreeNode left;
87+
* TreeNode right;
88+
* TreeNode() {}
89+
* TreeNode(int val) { this.val = val; }
90+
* TreeNode(int val, TreeNode left, TreeNode right) {
91+
* this.val = val;
92+
* this.left = left;
93+
* this.right = right;
94+
* }
95+
* }
96+
*/
97+
class Solution {
98+
public int sumRootToLeaf(TreeNode root) {
99+
return dfs(root, 0);
100+
}
101+
102+
private int dfs(TreeNode root, int t) {
103+
if (root == null) {
104+
return 0;
105+
}
106+
t = (t << 1) | root.val;
107+
if (root.left == null && root.right == null) {
108+
return t;
109+
}
110+
return dfs(root.left, t) + dfs(root.right, t);
111+
}
112+
}
113+
```
114+
115+
### **C++**
116+
117+
```cpp
118+
/**
119+
* Definition for a binary tree node.
120+
* struct TreeNode {
121+
* int val;
122+
* TreeNode *left;
123+
* TreeNode *right;
124+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
125+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
126+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
127+
* };
128+
*/
129+
class Solution {
130+
public:
131+
int sumRootToLeaf(TreeNode* root) {
132+
return dfs(root, 0);
133+
}
134+
135+
int dfs(TreeNode* root, int t) {
136+
if (!root) return 0;
137+
t = (t << 1) | root->val;
138+
if (!root->left && !root->right) return t;
139+
return dfs(root->left, t) + dfs(root->right, t);
140+
}
141+
};
142+
```
65143

144+
### **Go**
145+
146+
```go
147+
/**
148+
* Definition for a binary tree node.
149+
* type TreeNode struct {
150+
* Val int
151+
* Left *TreeNode
152+
* Right *TreeNode
153+
* }
154+
*/
155+
func sumRootToLeaf(root *TreeNode) int {
156+
var dfs func(root *TreeNode, t int) int
157+
dfs = func(root *TreeNode, t int) int {
158+
if root == nil {
159+
return 0
160+
}
161+
t = (t << 1) | root.Val
162+
if root.Left == nil && root.Right == nil {
163+
return t
164+
}
165+
return dfs(root.Left, t) + dfs(root.Right, t)
166+
}
167+
168+
return dfs(root, 0)
169+
}
66170
```
67171

68172
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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 sumRootToLeaf(TreeNode* root) {
15+
return dfs(root, 0);
16+
}
17+
18+
int dfs(TreeNode* root, int t) {
19+
if (!root) return 0;
20+
t = (t << 1) | root->val;
21+
if (!root->left && !root->right) return t;
22+
return dfs(root->left, t) + dfs(root->right, t);
23+
}
24+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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 sumRootToLeaf(root *TreeNode) int {
10+
var dfs func(root *TreeNode, t int) int
11+
dfs = func(root *TreeNode, t int) int {
12+
if root == nil {
13+
return 0
14+
}
15+
t = (t << 1) | root.Val
16+
if root.Left == nil && root.Right == nil {
17+
return t
18+
}
19+
return dfs(root.Left, t) + dfs(root.Right, t)
20+
}
21+
22+
return dfs(root, 0)
23+
}

0 commit comments

Comments
 (0)