diff --git a/Index/BFS.md b/Index/BFS.md index 8dbc578b..17a548cb 100644 --- a/Index/BFS.md +++ b/Index/BFS.md @@ -20,5 +20,6 @@ | [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/) | 简单 | 🤩🤩🤩🤩 | +| [1161. 最大层内元素和](https://leetcode.cn/problems/maximum-level-sum-of-a-binary-tree/) | [LeetCode 题解链接](https://leetcode.cn/problems/maximum-level-sum-of-a-binary-tree/solution/by-ac_oier-aemz/) | 中等 | 🤩🤩🤩🤩 | | [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/LeetCode/1161-1170/1161. \346\234\200\345\244\247\345\261\202\345\206\205\345\205\203\347\264\240\345\222\214\357\274\210\344\270\255\347\255\211\357\274\211.md" "b/LeetCode/1161-1170/1161. \346\234\200\345\244\247\345\261\202\345\206\205\345\205\203\347\264\240\345\222\214\357\274\210\344\270\255\347\255\211\357\274\211.md" new file mode 100644 index 00000000..0c5e3c3f --- /dev/null +++ "b/LeetCode/1161-1170/1161. \346\234\200\345\244\247\345\261\202\345\206\205\345\205\203\347\264\240\345\222\214\357\274\210\344\270\255\347\255\211\357\274\211.md" @@ -0,0 +1,105 @@ +### 题目描述 + +这是 LeetCode 上的 **[1161. 最大层内元素和](https://leetcode.cn/problems/maximum-level-sum-of-a-binary-tree/solution/by-ac_oier-aemz/)** ,难度为 **中等**。 + +Tag : 「层序遍历」、「BFS」 + + + +给你一个二叉树的根节点 `root`。设根节点位于二叉树的第 $1$ 层,而根节点的子节点位于第 $2$ 层,依此类推。 + +请返回层内元素之和 最大 的那几层(可能只有一层)的层号,并返回其中 最小 的那个。 + +示例 1: +![](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/08/17/capture.jpeg) +``` +输入:root = [1,7,0,7,-8,null,null] + +输出:2 + +解释: +第 1 层各元素之和为 1, +第 2 层各元素之和为 7 + 0 = 7, +第 3 层各元素之和为 7 + -8 = -1, +所以我们返回第 2 层的层号,它的层内元素之和最大。 +``` +示例 2: +``` +输入:root = [989,null,10250,98693,-89388,null,null,null,-32127] + +输出:2 +``` + +提示: +* 树中的节点数在 $[1, 10^4]$范围内 +* $-10^5 <= Node.val <= 10^5$ + +--- + +### 层序遍历 + +根据题意,使用 `BFS` 进行层序遍历即可。 + +每次以「层」为单位进行拓展,统计该层的元素和,维护处理过程中的最大值层数和,以及层深度。 + +Java 代码: +```Java +class Solution { + public int maxLevelSum(TreeNode root) { + Deque d = new ArrayDeque<>(); + int max = -0x3f3f3f3f, depth = 1, ans = 0; + d.addLast(root); + while (!d.isEmpty()) { + int sz = d.size(), cur = 0; + while (sz-- > 0) { + TreeNode t = d.pollFirst(); + if (t.left != null) d.addLast(t.left); + if (t.right != null) d.addLast(t.right); + cur += t.val; + } + if (cur > max) { + max = cur; ans = depth; + } + depth++; + } + return ans; + } +} +``` +TypeScript 代码: +```TypeScript +function maxLevelSum(root: TreeNode | null): number { + const d: TreeNode[] = new Array() + let he = 0, ta = 0 + d[ta++] = root + let max = -0x3f3f3f3f, depth = 1, ans = 0 + while (he < ta) { + let sz = ta - he, cur = 0 + while (sz-- > 0) { + const t = d[he++] + if (t.left != null) d[ta++] = t.left + if (t.right != null) d[ta++] = t.right + cur += t.val + } + if (cur > max) { + max = cur; ans = depth + } + depth++ + } + return ans +}; +``` +* 时间复杂度:$O(n)$ +* 空间复杂度:$O(n)$ + +--- + +### 最后 + +这是我们「刷穿 LeetCode」系列文章的第 `No.1161` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。 + +在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。 + +为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。 + +在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。 \ No newline at end of file