Skip to content

Commit

Permalink
✨feat: Add 1022
Browse files Browse the repository at this point in the history
  • Loading branch information
SharingSource committed May 30, 2022
1 parent 9cdea7b commit e9b7c47
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 1 deletion.
1 change: 1 addition & 0 deletions Index/BFS.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@
| [938. 二叉搜索树的范围和](https://leetcode-cn.com/problems/range-sum-of-bst/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/range-sum-of-bst/solution/gong-shui-san-xie-yi-ti-shuang-jie-di-gu-q2fo/) | 简单 | 🤩🤩🤩 |
| [965. 单值二叉树](https://leetcode.cn/problems/univalued-binary-tree/) | [LeetCode 题解链接](https://leetcode.cn/problems/univalued-binary-tree/solution/by-ac_oier-zxjl/) | 简单 | 🤩🤩🤩🤩 |
| [993. 二叉树的堂兄弟节点](https://leetcode-cn.com/problems/cousins-in-binary-tree/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/cousins-in-binary-tree/solution/gong-shui-san-xie-shu-de-sou-suo-dfs-bfs-b200/) | 简单 | 🤩🤩 |
| [1022. 从根到叶的二进制数之和](https://leetcode.cn/problems/sum-of-root-to-leaf-binary-numbers/) | [LeetCode 题解链接](https://leetcode.cn/problems/sum-of-root-to-leaf-binary-numbers/solution/by-ac_oier-1905/) | 简单 | 🤩🤩🤩🤩 |
| [1609. 奇偶树](https://leetcode-cn.com/problems/even-odd-tree/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/even-odd-tree/solution/gong-shui-san-xie-yi-ti-shuang-jie-bfs-d-kuyi/) | 中等 | 🤩🤩🤩🤩🤩 |

1 change: 1 addition & 0 deletions Index/DFS.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
| [965. 单值二叉树](https://leetcode.cn/problems/univalued-binary-tree/) | [LeetCode 题解链接](https://leetcode.cn/problems/univalued-binary-tree/solution/by-ac_oier-zxjl/) | 简单 | 🤩🤩🤩🤩 |
| [987. 二叉树的垂序遍历](https://leetcode-cn.com/problems/vertical-order-traversal-of-a-binary-tree/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/vertical-order-traversal-of-a-binary-tree/solution/gong-shui-san-xie-yi-ti-shuang-jie-dfs-h-wfm3/) | 困难 | 🤩🤩🤩🤩🤩 |
| [993. 二叉树的堂兄弟节点](https://leetcode-cn.com/problems/cousins-in-binary-tree/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/cousins-in-binary-tree/solution/gong-shui-san-xie-shu-de-sou-suo-dfs-bfs-b200/) | 简单 | 🤩🤩 |
| [1022. 从根到叶的二进制数之和](https://leetcode.cn/problems/sum-of-root-to-leaf-binary-numbers/) | [LeetCode 题解链接](https://leetcode.cn/problems/sum-of-root-to-leaf-binary-numbers/solution/by-ac_oier-1905/) | 简单 | 🤩🤩🤩🤩 |
| [1239. 串联字符串的最大长度](https://leetcode-cn.com/problems/maximum-length-of-a-concatenated-string-with-unique-characters/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximum-length-of-a-concatenated-string-with-unique-characters/solution/gong-shui-san-xie-yi-ti-san-jie-jian-zhi-nfeb/) | 中等 | 🤩🤩🤩 |
| [1609. 奇偶树](https://leetcode-cn.com/problems/even-odd-tree/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/even-odd-tree/solution/gong-shui-san-xie-yi-ti-shuang-jie-bfs-d-kuyi/) | 中等 | 🤩🤩🤩🤩🤩 |
| [1723. 完成所有工作的最短时间](https://leetcode-cn.com/problems/find-minimum-time-to-finish-all-jobs/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/find-minimum-time-to-finish-all-jobs/solution/gong-shui-san-xie-yi-ti-shuang-jie-jian-4epdd/) | 困难 | 🤩🤩🤩 |
Expand Down
1 change: 1 addition & 0 deletions Index/二叉树.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
| [965. 单值二叉树](https://leetcode.cn/problems/univalued-binary-tree/) | [LeetCode 题解链接](https://leetcode.cn/problems/univalued-binary-tree/solution/by-ac_oier-zxjl/) | 简单 | 🤩🤩🤩🤩🤩 |
| [987. 二叉树的垂序遍历](https://leetcode-cn.com/problems/vertical-order-traversal-of-a-binary-tree/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/vertical-order-traversal-of-a-binary-tree/solution/gong-shui-san-xie-yi-ti-shuang-jie-dfs-h-wfm3/) | 困难 | 🤩🤩🤩🤩🤩 |
| [993. 二叉树的堂兄弟节点](https://leetcode-cn.com/problems/cousins-in-binary-tree/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/cousins-in-binary-tree/solution/gong-shui-san-xie-shu-de-sou-suo-dfs-bfs-b200/) | 简单 | 🤩🤩 |
| [1022. 从根到叶的二进制数之和](https://leetcode.cn/problems/sum-of-root-to-leaf-binary-numbers/) | [LeetCode 题解链接](https://leetcode.cn/problems/sum-of-root-to-leaf-binary-numbers/solution/by-ac_oier-1905/) | 简单 | 🤩🤩🤩🤩 |
| [1104. 二叉树寻路](https://leetcode-cn.com/problems/path-in-zigzag-labelled-binary-tree/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/path-in-zigzag-labelled-binary-tree/solution/gong-shui-san-xie-yi-ti-shuang-jie-mo-ni-rw2d/) | 中等 | 🤩🤩🤩 |
| [1305. 两棵二叉搜索树中的所有元素](https://leetcode-cn.com/problems/all-elements-in-two-binary-search-trees/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/all-elements-in-two-binary-search-trees/solution/by-ac_oier-c8fv/) | 中等 | 🤩🤩🤩🤩 |
| [剑指 Offer 37. 序列化二叉树](https://leetcode-cn.com/problems/xu-lie-hua-er-cha-shu-lcof/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/xu-lie-hua-er-cha-shu-lcof/solution/gong-shui-san-xie-er-cha-shu-de-xu-lie-h-n89a/) | 困难 | 🤩🤩🤩🤩🤩 |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
### 题目描述

这是 LeetCode 上的 **[1022. 从根到叶的二进制数之和](https://leetcode.cn/problems/sum-of-root-to-leaf-binary-numbers/solution/by-ac_oier-1905/)** ,难度为 **简单**

Tag : 「DFS」、「BFS」、「二叉树」、「树的遍历」



给出一棵二叉树,其上每个结点的值都是 $0$ 或 $1$ 。每一条从根到叶的路径都代表一个从最高有效位开始的二进制数。

例如,如果路径为 `0 -> 1 -> 1 -> 0 -> 1`,那么它表示二进制数 `01101`,也就是 $13$ 。
对树上的每一片叶子,我们都要找出从根到该叶子的路径所表示的数字。

返回这些数字之和。题目数据保证答案是一个 $32$ 位 整数。

示例 1:
![](https://assets.leetcode.com/uploads/2019/04/04/sum-of-root-to-leaf-binary-numbers.png)
```
输入:root = [1,0,1,0,1,0,1]
输出:22
解释:(100) + (101) + (110) + (111) = 4 + 5 + 6 + 7 = 22
```
示例 2:
```
输入:root = [0]
输出:0
```

提示:
* 树中的节点数在 $[1, 1000]$ 范围内
* `Node.val` 仅为 $0$ 或 $1$ 

---

### 递归

容易想到「递归」进行求解,在 `DFS` 过程中记录下当前的值为多少,假设遍历到当前节点 $x$ 前,记录的值为 $cur$,那么根据题意,我们需要先将 $cur$ 进行整体左移(腾出最后一位),然后将节点 $x$ 的值放置最低位来得到新的值,并继续进行递归。

递归有使用「函数返回值」和「全局变量」两种实现方式。

代码:
```Java
class Solution {
public int sumRootToLeaf(TreeNode root) {
return dfs(root, 0);
}
int dfs(TreeNode root, int cur) {
int ans = 0, ncur = (cur << 1) + root.val;
if (root.left != null) ans += dfs(root.left, ncur);
if (root.right != null) ans += dfs(root.right, ncur);
return root.left == null && root.right == null ? ncur : ans;
}
}
```

-

```Java
class Solution {
int ans = 0;
public int sumRootToLeaf(TreeNode root) {
dfs(root, 0);
return ans;
}
void dfs(TreeNode root, int cur) {
int ncur = (cur << 1) + root.val;
if (root.left != null) dfs(root.left, ncur);
if (root.right != null) dfs(root.right, ncur);
if (root.left == null && root.right == null) ans += ncur;
}
}
```
* 时间复杂度:$O(n)$
* 空间复杂度:忽略递归带来的额外空间开销,复杂度为 $O(1)$

---

### 迭代

自然也可以使用「迭代」进行求解。

为了不引入除「队列」以外的其他数据结构,当我们可以把某个节点 $x$ 放出队列时,先将其的值修改为当前遍历路径对应的二进制数。

代码:
```Java
class Solution {
public int sumRootToLeaf(TreeNode root) {
int ans = 0;
Deque<TreeNode> d = new ArrayDeque<>();
d.addLast(root);
while (!d.isEmpty()) {
TreeNode poll = d.pollFirst();
if (poll.left != null) {
poll.left.val = (poll.val << 1) + poll.left.val;
d.addLast(poll.left);
}
if (poll.right != null) {
poll.right.val = (poll.val << 1) + poll.right.val;
d.addLast(poll.right);
}
if (poll.left == null && poll.right == null) ans += poll.val;
}
return ans;
}
}
```
* 时间复杂度:$O(n)$
* 空间复杂度:$O(n)$

---

### 最后

这是我们「刷穿 LeetCode」系列文章的第 `No.1022` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。

在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。

为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode

在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

这是 LeetCode 上的 **[783. 二叉搜索树节点最小距离](https://leetcode-cn.com/problems/minimum-distance-between-bst-nodes/solution/gong-shui-san-xie-yi-ti-san-jie-shu-de-s-7r17/)** ,难度为 **简单**

Tag : 「树的搜素」、「迭代」、「非迭代」、「中序遍历」、「BFS」、「DFS」
Tag : 「树的搜索」、「迭代」、「非迭代」、「中序遍历」、「BFS」、「DFS」



Expand Down

0 comments on commit e9b7c47

Please sign in to comment.