Skip to content

Commit f85dbb9

Browse files
authored
feat: add solutions for lc No.1022 (#5044)
1 parent 6b4bc4e commit f85dbb9

8 files changed

Lines changed: 173 additions & 146 deletions

File tree

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

Lines changed: 57 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@ tags:
6464

6565
### 方法一:递归
6666

67-
我们设计一个递归函数 `dfs(root, t)`,它接收两个参数:当前节点 `root` 和当前节点的父节点对应的二进制数 `t`。函数的返回值是从当前节点到叶子节点的路径所表示的二进制数之和。答案即为 `dfs(root, 0)`
67+
我们设计一个递归函数 $\text{dfs}(root, t)$,它接收两个参数:当前节点 $root$ 和当前节点的父节点对应的二进制数 $t$。函数的返回值是从当前节点到叶子节点的路径所表示的二进制数之和。答案即为 $\textrm{dfs}(root, 0)$
6868

6969
递归函数的逻辑如下:
7070

71-
- 如果当前节点 `root` 为空,则返回 `0`,否则计算当前节点对应的二进制数 `t`,即 `t = t << 1 | root.val`
72-
- 如果当前节点是叶子节点,则返回 `t`,否则返回 `dfs(root.left, t)``dfs(root.right, t)` 的和。
71+
- 如果当前节点 $root$ 为空,则返回 $0$,否则计算当前节点对应的二进制数 $t$,即 $t = t \ll 1 | root.val$
72+
- 如果当前节点是叶子节点,则返回 $t$,否则返回 $\textrm{dfs}(root.left, t)$$\textrm{dfs}(root.right, t)$ 的和。
7373

7474
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是二叉树的节点数。对每个节点访问一次;递归栈需要 $O(n)$ 的空间。
7575

@@ -85,14 +85,14 @@ tags:
8585
# self.left = left
8686
# self.right = right
8787
class Solution:
88-
def sumRootToLeaf(self, root: TreeNode) -> int:
89-
def dfs(root, t):
88+
def sumRootToLeaf(self, root: Optional[TreeNode]) -> int:
89+
def dfs(root: Optional[TreeNode], x: int) -> int:
9090
if root is None:
9191
return 0
92-
t = (t << 1) | root.val
93-
if root.left is None and root.right is None:
94-
return t
95-
return dfs(root.left, t) + dfs(root.right, t)
92+
x = x << 1 | root.val
93+
if root.left == root.right:
94+
return x
95+
return dfs(root.left, x) + dfs(root.right, x)
9696

9797
return dfs(root, 0)
9898
```
@@ -120,15 +120,15 @@ class Solution {
120120
return dfs(root, 0);
121121
}
122122

123-
private int dfs(TreeNode root, int t) {
123+
private int dfs(TreeNode root, int x) {
124124
if (root == null) {
125125
return 0;
126126
}
127-
t = (t << 1) | root.val;
128-
if (root.left == null && root.right == null) {
129-
return t;
127+
x = x << 1 | root.val;
128+
if (root.left == root.right) {
129+
return x;
130130
}
131-
return dfs(root.left, t) + dfs(root.right, t);
131+
return dfs(root.left, x) + dfs(root.right, x);
132132
}
133133
}
134134
```
@@ -150,15 +150,18 @@ class Solution {
150150
class Solution {
151151
public:
152152
int sumRootToLeaf(TreeNode* root) {
153+
auto dfs = [&](this auto&& dfs, TreeNode* root, int x) -> int {
154+
if (!root) {
155+
return 0;
156+
}
157+
x = x << 1 | root->val;
158+
if (root->left == root->right) {
159+
return x;
160+
}
161+
return dfs(root->left, x) + dfs(root->right, x);
162+
};
153163
return dfs(root, 0);
154164
}
155-
156-
int dfs(TreeNode* root, int t) {
157-
if (!root) return 0;
158-
t = (t << 1) | root->val;
159-
if (!root->left && !root->right) return t;
160-
return dfs(root->left, t) + dfs(root->right, t);
161-
}
162165
};
163166
```
164167
@@ -174,18 +177,17 @@ public:
174177
* }
175178
*/
176179
func sumRootToLeaf(root *TreeNode) int {
177-
var dfs func(root *TreeNode, t int) int
178-
dfs = func(root *TreeNode, t int) int {
180+
var dfs func(*TreeNode, int) int
181+
dfs = func(root *TreeNode, x int) int {
179182
if root == nil {
180183
return 0
181184
}
182-
t = (t << 1) | root.Val
183-
if root.Left == nil && root.Right == nil {
184-
return t
185+
x = x<<1 | root.Val
186+
if root.Left == root.Right {
187+
return x
185188
}
186-
return dfs(root.Left, t) + dfs(root.Right, t)
189+
return dfs(root.Left, x) + dfs(root.Right, x)
187190
}
188-
189191
return dfs(root, 0)
190192
}
191193
```
@@ -208,17 +210,20 @@ func sumRootToLeaf(root *TreeNode) int {
208210
*/
209211

210212
function sumRootToLeaf(root: TreeNode | null): number {
211-
const dfs = (root: TreeNode | null, num: number) => {
212-
if (root == null) {
213+
const dfs = (node: TreeNode | null, x: number): number => {
214+
if (node === null) {
213215
return 0;
214216
}
215-
const { val, left, right } = root;
216-
num = (num << 1) | val;
217-
if (left == null && right == null) {
218-
return num;
217+
218+
x = (x << 1) | node.val;
219+
220+
if (node.left === null && node.right === null) {
221+
return x;
219222
}
220-
return dfs(left, num) + dfs(right, num);
223+
224+
return dfs(node.left, x) + dfs(node.right, x);
221225
};
226+
222227
return dfs(root, 0);
223228
}
224229
```
@@ -244,23 +249,27 @@ function sumRootToLeaf(root: TreeNode | null): number {
244249
// }
245250
// }
246251
// }
247-
use std::cell::RefCell;
248252
use std::rc::Rc;
253+
use std::cell::RefCell;
254+
249255
impl Solution {
250-
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>, mut num: i32) -> i32 {
251-
if root.is_none() {
252-
return 0;
253-
}
254-
let root = root.as_ref().unwrap().borrow();
255-
num = (num << 1) | root.val;
256-
if root.left.is_none() && root.right.is_none() {
257-
return num;
256+
pub fn sum_root_to_leaf(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
257+
fn dfs(node: Option<Rc<RefCell<TreeNode>>>, x: i32) -> i32 {
258+
if let Some(n) = node {
259+
let n_ref = n.borrow();
260+
let x = (x << 1) | n_ref.val;
261+
262+
if n_ref.left.is_none() && n_ref.right.is_none() {
263+
return x;
264+
}
265+
266+
dfs(n_ref.left.clone(), x) + dfs(n_ref.right.clone(), x)
267+
} else {
268+
0
269+
}
258270
}
259-
Self::dfs(&root.left, num) + Self::dfs(&root.right, num)
260-
}
261271

262-
pub fn sum_root_to_leaf(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
263-
Self::dfs(&root, 0)
272+
dfs(root, 0)
264273
}
265274
}
266275
```

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

Lines changed: 58 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,14 @@ tags:
6262

6363
### Solution 1: Recursion
6464

65-
We design a recursive function `dfs(root, t)`, which takes two parameters: the current node `root` and the binary number corresponding to the parent node `t`. The return value of the function is the sum of the binary numbers represented by the path from the current node to the leaf node. The answer is `dfs(root, 0)`.
65+
We design a recursive function $\text{dfs}(root, t)$, which takes two parameters: the current node $root$ and the binary number $t$ corresponding to the parent node of the current node. The return value of the function is the sum of binary numbers represented by paths from the current node to leaf nodes. The answer is $\textrm{dfs}(root, 0)$.
6666

6767
The logic of the recursive function is as follows:
6868

69-
- If the current node `root` is null, then return `0`. Otherwise, calculate the binary number `t` corresponding to the current node, i.e., `t = t << 1 | root.val`.
70-
- If the current node is a leaf node, then return `t`. Otherwise, return the sum of `dfs(root.left, t)` and `dfs(root.right, t)`.
69+
- If the current node $root$ is null, return $0$; otherwise, calculate the binary number $t$ corresponding to the current node, i.e., $t = t \ll 1 | root.val$.
70+
- If the current node is a leaf node, return $t$; otherwise, return the sum of $\textrm{dfs}(root.left, t)$ and $\textrm{dfs}(root.right, t)$.
7171

72-
The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the number of nodes in the binary tree. Each node is visited once; the recursion stack requires $O(n)$ space.
72+
The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the number of nodes in the binary tree. Each node is visited once; the recursion stack requires $O(n)$ space.
7373

7474
<!-- tabs:start -->
7575

@@ -83,14 +83,14 @@ The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is
8383
# self.left = left
8484
# self.right = right
8585
class Solution:
86-
def sumRootToLeaf(self, root: TreeNode) -> int:
87-
def dfs(root, t):
86+
def sumRootToLeaf(self, root: Optional[TreeNode]) -> int:
87+
def dfs(root: Optional[TreeNode], x: int) -> int:
8888
if root is None:
8989
return 0
90-
t = (t << 1) | root.val
91-
if root.left is None and root.right is None:
92-
return t
93-
return dfs(root.left, t) + dfs(root.right, t)
90+
x = x << 1 | root.val
91+
if root.left == root.right:
92+
return x
93+
return dfs(root.left, x) + dfs(root.right, x)
9494

9595
return dfs(root, 0)
9696
```
@@ -118,15 +118,15 @@ class Solution {
118118
return dfs(root, 0);
119119
}
120120

121-
private int dfs(TreeNode root, int t) {
121+
private int dfs(TreeNode root, int x) {
122122
if (root == null) {
123123
return 0;
124124
}
125-
t = (t << 1) | root.val;
126-
if (root.left == null && root.right == null) {
127-
return t;
125+
x = x << 1 | root.val;
126+
if (root.left == root.right) {
127+
return x;
128128
}
129-
return dfs(root.left, t) + dfs(root.right, t);
129+
return dfs(root.left, x) + dfs(root.right, x);
130130
}
131131
}
132132
```
@@ -148,15 +148,18 @@ class Solution {
148148
class Solution {
149149
public:
150150
int sumRootToLeaf(TreeNode* root) {
151+
auto dfs = [&](this auto&& dfs, TreeNode* root, int x) -> int {
152+
if (!root) {
153+
return 0;
154+
}
155+
x = x << 1 | root->val;
156+
if (root->left == root->right) {
157+
return x;
158+
}
159+
return dfs(root->left, x) + dfs(root->right, x);
160+
};
151161
return dfs(root, 0);
152162
}
153-
154-
int dfs(TreeNode* root, int t) {
155-
if (!root) return 0;
156-
t = (t << 1) | root->val;
157-
if (!root->left && !root->right) return t;
158-
return dfs(root->left, t) + dfs(root->right, t);
159-
}
160163
};
161164
```
162165
@@ -172,18 +175,17 @@ public:
172175
* }
173176
*/
174177
func sumRootToLeaf(root *TreeNode) int {
175-
var dfs func(root *TreeNode, t int) int
176-
dfs = func(root *TreeNode, t int) int {
178+
var dfs func(*TreeNode, int) int
179+
dfs = func(root *TreeNode, x int) int {
177180
if root == nil {
178181
return 0
179182
}
180-
t = (t << 1) | root.Val
181-
if root.Left == nil && root.Right == nil {
182-
return t
183+
x = x<<1 | root.Val
184+
if root.Left == root.Right {
185+
return x
183186
}
184-
return dfs(root.Left, t) + dfs(root.Right, t)
187+
return dfs(root.Left, x) + dfs(root.Right, x)
185188
}
186-
187189
return dfs(root, 0)
188190
}
189191
```
@@ -206,17 +208,20 @@ func sumRootToLeaf(root *TreeNode) int {
206208
*/
207209

208210
function sumRootToLeaf(root: TreeNode | null): number {
209-
const dfs = (root: TreeNode | null, num: number) => {
210-
if (root == null) {
211+
const dfs = (node: TreeNode | null, x: number): number => {
212+
if (node === null) {
211213
return 0;
212214
}
213-
const { val, left, right } = root;
214-
num = (num << 1) | val;
215-
if (left == null && right == null) {
216-
return num;
215+
216+
x = (x << 1) | node.val;
217+
218+
if (node.left === null && node.right === null) {
219+
return x;
217220
}
218-
return dfs(left, num) + dfs(right, num);
221+
222+
return dfs(node.left, x) + dfs(node.right, x);
219223
};
224+
220225
return dfs(root, 0);
221226
}
222227
```
@@ -242,23 +247,27 @@ function sumRootToLeaf(root: TreeNode | null): number {
242247
// }
243248
// }
244249
// }
245-
use std::cell::RefCell;
246250
use std::rc::Rc;
251+
use std::cell::RefCell;
252+
247253
impl Solution {
248-
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>, mut num: i32) -> i32 {
249-
if root.is_none() {
250-
return 0;
251-
}
252-
let root = root.as_ref().unwrap().borrow();
253-
num = (num << 1) | root.val;
254-
if root.left.is_none() && root.right.is_none() {
255-
return num;
254+
pub fn sum_root_to_leaf(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
255+
fn dfs(node: Option<Rc<RefCell<TreeNode>>>, x: i32) -> i32 {
256+
if let Some(n) = node {
257+
let n_ref = n.borrow();
258+
let x = (x << 1) | n_ref.val;
259+
260+
if n_ref.left.is_none() && n_ref.right.is_none() {
261+
return x;
262+
}
263+
264+
dfs(n_ref.left.clone(), x) + dfs(n_ref.right.clone(), x)
265+
} else {
266+
0
267+
}
256268
}
257-
Self::dfs(&root.left, num) + Self::dfs(&root.right, num)
258-
}
259269

260-
pub fn sum_root_to_leaf(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
261-
Self::dfs(&root, 0)
270+
dfs(root, 0)
262271
}
263272
}
264273
```

solution/1000-1099/1022.Sum of Root To Leaf Binary Numbers/Solution.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@
1212
class Solution {
1313
public:
1414
int sumRootToLeaf(TreeNode* root) {
15+
auto dfs = [&](this auto&& dfs, TreeNode* root, int x) -> int {
16+
if (!root) {
17+
return 0;
18+
}
19+
x = x << 1 | root->val;
20+
if (root->left == root->right) {
21+
return x;
22+
}
23+
return dfs(root->left, x) + dfs(root->right, x);
24+
};
1525
return dfs(root, 0);
1626
}
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-
};
27+
};

0 commit comments

Comments
 (0)