Skip to content

Commit 9889624

Browse files
committed
update: 添加问题“1022.从根到叶的二进制数之和”的代码(并更新其题解) (#1409)
1022: AC.cpp (#1408) - AC,100.00%,78.62% + en(@beijing) Signed-off-by: LetMeFly666 <Tisfy@qq.com>
1 parent b915669 commit 9889624

7 files changed

Lines changed: 123 additions & 39 deletions

.commitmsg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1461: AC.cpp (#1406) - AC,40.08%,36.38%
1+
1022: AC.cpp (#1408) - AC,100.00%,78.62% + en(@Beijing)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2026-02-24 21:34:35
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2026-02-24 21:37:38
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
/**
12+
* Definition for a binary tree node.
13+
* struct TreeNode {
14+
* int val;
15+
* TreeNode *left;
16+
* TreeNode *right;
17+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
18+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
19+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
20+
* };
21+
*/
22+
class Solution {
23+
private:
24+
int ans;
25+
26+
// @input.root not empty
27+
void dfs(TreeNode* root, int now) {
28+
now <<= 1;
29+
now += root->val;
30+
if (!root->left && !root->right) {
31+
ans += now;
32+
return;
33+
}
34+
if (root->left) {
35+
dfs(root->left, now);
36+
}
37+
if (root->right) {
38+
dfs(root->right, now);
39+
}
40+
}
41+
public:
42+
int sumRootToLeaf(TreeNode* root) {
43+
ans = 0;
44+
dfs(root, 0);
45+
return ans;
46+
}
47+
};

Codes/1461-check-if-a-string-contains-all-binary-codes-of-size-k.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,18 @@
22
* @Author: LetMeFly
33
* @Date: 2026-02-23 11:30:05
44
* @LastEditors: LetMeFly.xyz
5-
* @LastEditTime: 2026-02-23 11:37:00
5+
* @LastEditTime: 2026-02-23 11:37:10
66
*/
77
#if defined(_WIN32) || defined(__APPLE__)
88
#include "_[1,2]toVector.h"
99
#endif
10-
// THIS CANNOT BE ACCEPTED
10+
1111
class Solution {
1212
public:
1313
bool hasAllCodes(string s, int k) {
1414
unordered_set<string> ma;
15-
for (int i = 0, n = s.size(); i < n; i++) {
16-
for (int l = 1; l <= k && i + l <= n; l++) {
17-
ma.insert(s.substr(i, l));
18-
}
15+
for (int i = 0, to = s.size() - k + 1; i < to; i++) {
16+
ma.insert(s.substr(i, k));
1917
}
2018
return ma.size() == (1 << k);
2119
}

Codes/1461-check-if-a-string-contains-all-binary-codes-of-size-k_AC.cpp

Lines changed: 0 additions & 20 deletions
This file was deleted.

Solutions/LeetCode 1022.从根到叶的二进制数之和.md

Lines changed: 65 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
---
2-
title: 1022.从根到叶的二进制数之和
2+
title: 1022.从根到叶的二进制数之和:DFS或BFS
33
date: 2022-05-30 10:52:40
4-
tags: [题解, LeetCode, 简单, 二叉树, 树, 交互, 模拟]
4+
tags: [题解, LeetCode, 简单, 二叉树, 树, 模拟, 深度优先搜索, DFS, 广度优先搜索, BFS, 递归, 位运算]
55
categories: [题解, LeetCode]
6+
index_img: https://assets.leetcode.com/uploads/2019/04/04/sum-of-root-to-leaf-binary-numbers.png
67
---
78

8-
# 【LetMeFly】1022.从根到叶的二进制数之和
9+
# 【LetMeFly】1022.从根到叶的二进制数之和:DFS或BFS
910

1011
力扣题目链接:[https://leetcode.cn/problems/sum-of-root-to-leaf-binary-numbers/](https://leetcode.cn/problems/sum-of-root-to-leaf-binary-numbers/)
1112

@@ -44,12 +45,12 @@ categories: [题解, LeetCode]
4445

4546
二进制的100等于十进制的4
4647

47-
# 思路
48-
49-
我们只需要层次遍历这棵树,遍历到某个节点时,如果存在子节点,子节点就加上这个节点的“值的<<1”的结果
48+
# 解题方法
5049

5150
## 方法一:层次遍历
5251

52+
我们只需要层次遍历这棵树,遍历到某个节点时,如果存在子节点,子节点就加上这个节点的“值的<<1”的结果
53+
5354
+ 时间复杂度$O(n)$,其中$n$是树中节点的数量
5455
+ 空间复杂度$O(n)$
5556

@@ -58,6 +59,9 @@ categories: [题解, LeetCode]
5859
#### C++
5960

6061
```cpp
62+
/*
63+
* @LastEditTime: 2022-05-30 10:50:08
64+
*/
6165
class Solution {
6266
public:
6367
int sumRootToLeaf(TreeNode* root) { // 不会为空
@@ -86,5 +90,58 @@ public:
8690
};
8791
```
8892
89-
> 同步发文于CSDN,原创不易,转载请附上[原文链接](https://blog.letmefly.xyz/2022/05/30/LeetCode%201022.%E4%BB%8E%E6%A0%B9%E5%88%B0%E5%8F%B6%E7%9A%84%E4%BA%8C%E8%BF%9B%E5%88%B6%E6%95%B0%E4%B9%8B%E5%92%8C)哦~
90-
> Tisfy:[https://letmefly.blog.csdn.net/article/details/125043202](https://letmefly.blog.csdn.net/article/details/125043202)
93+
## 方法二:深度优先搜索DFS
94+
95+
使用一个类中的“全局变量”$ans$记录所有路径数字之和,写一个DFS函数:
96+
97+
```cpp
98+
void dfs(TreeNode* root, int now)
99+
```
100+
101+
其中`now`代表该走节点`root`时已经走过路径的二进制值。
102+
103+
首先更新走到当前节点后的路径值`now = now << 1 + root->val`,如果`root`为叶节点则将该路径值累加到$ans$,否则继续递归左右子中非空的节点。
104+
105+
+ 时间复杂度$O(n)$,其中$n$是树中节点的数量
106+
+ 空间复杂度$O(n)$
107+
108+
### AC代码
109+
110+
#### C++
111+
112+
```cpp
113+
/*
114+
* @LastEditTime: 2026-02-24 21:37:38
115+
*/
116+
class Solution {
117+
private:
118+
int ans;
119+
120+
// @input.root not empty
121+
void dfs(TreeNode* root, int now) {
122+
now <<= 1;
123+
now += root->val;
124+
if (!root->left && !root->right) {
125+
ans += now;
126+
return;
127+
}
128+
if (root->left) {
129+
dfs(root->left, now);
130+
}
131+
if (root->right) {
132+
dfs(root->right, now);
133+
}
134+
}
135+
public:
136+
int sumRootToLeaf(TreeNode* root) {
137+
ans = 0;
138+
dfs(root, 0);
139+
return ans;
140+
}
141+
};
142+
143+
```
144+
145+
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/125043202)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](ttps://blog.letmefly.xyz/2022/05/30/LeetCode%201022.%E4%BB%8E%E6%A0%B9%E5%88%B0%E5%8F%B6%E7%9A%84%E4%BA%8C%E8%BF%9B%E5%88%B6%E6%95%B0%E4%B9%8B%E5%92%8C)哦~
146+
>
147+
> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode)

Solutions/Other-English-LearningNotes-SomeWords.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1831,6 +1831,10 @@ categories: [自用]
18311831
|stipulation|n. 规定,约定,合同|
18321832
|||
18331833
|turnip|n. 萝卜|
1834+
|||
1835+
|senseless|adj. 无意义的,无目的的;失去知觉的;不明智的,愚蠢的|
1836+
|enlightening|v. 启迪,指导<br/>adj. 有启迪的,使人感悟的|
1837+
|potent|adj. 强有效的,有力的,烈性的,影响身心的|
18341838

18351839
+ 这个web要是能设计得可以闭眼(完全不睁眼)键盘控制背单词就好了。
18361840
+ 也许可以加个AI用最近词编故事功能(返回接口中支持标注所使用单词高亮?)
@@ -1892,4 +1896,4 @@ categories: [自用]
18921896
</script>
18931897

18941898
> 原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2024/02/19/Other-English-LearningNotes-SomeWords/)~
1895-
> [https://blog.letmefly.xyz/2024/02/19/Other-English-LearningNotes-SomeWords/](https://blog.letmefly.xyz/2024/02/19/Other-English-LearningNotes-SomeWords/)
1899+
> [https://blog.letmefly.xyz/2024/02/19/Other-English-LearningNotes-SomeWords/](https://blog.letmefly.xyz/2024/02/19/Other-English-LearningNotes-SomeWords/)

todo

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# rm Codes\1461-check-if-a-string-contains-all-binary-codes-of-size-k_AC.cpp
2-
31
# 记录iphone快捷指令 自动化 当到达某位置时触发
42

53
# move beautify.sh to github repo/gist
@@ -120,4 +118,4 @@ def get_environ_proxies(url, no_proxy=None):
120118
else:
121119
return getproxies()
122120

123-
print(get_environ_proxies("https://letmefly.xyz"))
121+
print(get_environ_proxies("https://letmefly.xyz"))

0 commit comments

Comments
 (0)