From e9b7c47d1d3018c1eaa4fb21ed6229fe13db3119 Mon Sep 17 00:00:00 2001 From: AC_Oier Date: Mon, 30 May 2022 09:41:30 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8feat:=20Add=201022?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Index/BFS.md | 1 + Index/DFS.md | 1 + .../\344\272\214\345\217\211\346\240\221.md" | 1 + ...10\347\256\200\345\215\225\357\274\211.md" | 124 ++++++++++++++++++ ...10\347\256\200\345\215\225\357\274\211.md" | 2 +- 5 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 "LeetCode/1021-1030/1022. \344\273\216\346\240\271\345\210\260\345\217\266\347\232\204\344\272\214\350\277\233\345\210\266\346\225\260\344\271\213\345\222\214\357\274\210\347\256\200\345\215\225\357\274\211.md" diff --git a/Index/BFS.md b/Index/BFS.md index f94d39f5..53a584da 100644 --- a/Index/BFS.md +++ b/Index/BFS.md @@ -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/) | 中等 | 🤩🤩🤩🤩🤩 | diff --git a/Index/DFS.md b/Index/DFS.md index 8f3398a3..099a302f 100644 --- a/Index/DFS.md +++ b/Index/DFS.md @@ -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/) | 困难 | 🤩🤩🤩 | diff --git "a/Index/\344\272\214\345\217\211\346\240\221.md" "b/Index/\344\272\214\345\217\211\346\240\221.md" index ecbbcc89..5bc4b009 100644 --- "a/Index/\344\272\214\345\217\211\346\240\221.md" +++ "b/Index/\344\272\214\345\217\211\346\240\221.md" @@ -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/) | 困难 | 🤩🤩🤩🤩🤩 | diff --git "a/LeetCode/1021-1030/1022. \344\273\216\346\240\271\345\210\260\345\217\266\347\232\204\344\272\214\350\277\233\345\210\266\346\225\260\344\271\213\345\222\214\357\274\210\347\256\200\345\215\225\357\274\211.md" "b/LeetCode/1021-1030/1022. \344\273\216\346\240\271\345\210\260\345\217\266\347\232\204\344\272\214\350\277\233\345\210\266\346\225\260\344\271\213\345\222\214\357\274\210\347\256\200\345\215\225\357\274\211.md" new file mode 100644 index 00000000..43227af7 --- /dev/null +++ "b/LeetCode/1021-1030/1022. \344\273\216\346\240\271\345\210\260\345\217\266\347\232\204\344\272\214\350\277\233\345\210\266\346\225\260\344\271\213\345\222\214\357\274\210\347\256\200\345\215\225\357\274\211.md" @@ -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 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 原题链接和其他优选题解。 + diff --git "a/LeetCode/781-790/783. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\212\202\347\202\271\346\234\200\345\260\217\350\267\235\347\246\273\357\274\210\347\256\200\345\215\225\357\274\211.md" "b/LeetCode/781-790/783. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\212\202\347\202\271\346\234\200\345\260\217\350\267\235\347\246\273\357\274\210\347\256\200\345\215\225\357\274\211.md" index 99058c0b..e1fd8df9 100644 --- "a/LeetCode/781-790/783. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\212\202\347\202\271\346\234\200\345\260\217\350\267\235\347\246\273\357\274\210\347\256\200\345\215\225\357\274\211.md" +++ "b/LeetCode/781-790/783. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\212\202\347\202\271\346\234\200\345\260\217\350\267\235\347\246\273\357\274\210\347\256\200\345\215\225\357\274\211.md" @@ -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」