diff --git a/Index/DFS.md b/Index/DFS.md index 0b9d9313..20c2384b 100644 --- a/Index/DFS.md +++ b/Index/DFS.md @@ -13,6 +13,7 @@ | [437. 路径总和 III](https://leetcode-cn.com/problems/path-sum-iii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/path-sum-iii/solution/gong-shui-san-xie-yi-ti-shuang-jie-dfs-q-usa7/) | 中等 | 🤩🤩🤩🤩 | | [488. 祖玛游戏](https://leetcode-cn.com/problems/zuma-game/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/zuma-game/solution/gong-shui-san-xie-yi-ti-shuang-jie-sou-s-3ftb/) | 困难 | 🤩🤩🤩🤩 | | [494. 目标和](https://leetcode-cn.com/problems/target-sum/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/target-sum/solution/gong-shui-san-xie-yi-ti-si-jie-dfs-ji-yi-et5b/) | 中等 | 🤩🤩🤩🤩 | +| [563. 二叉树的坡度](https://leetcode-cn.com/problems/binary-tree-tilt/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/binary-tree-tilt/solution/gong-shui-san-xie-jian-dan-er-cha-shu-di-ekz4/) | 简单 | 🤩🤩🤩🤩 | | [638. 大礼包](https://leetcode-cn.com/problems/shopping-offers/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/shopping-offers/solution/gong-shui-san-xie-yi-ti-shuang-jie-zhuan-qgk1/) | 中等 | 🤩🤩🤩🤩 | | [677. 键值映射](https://leetcode-cn.com/problems/map-sum-pairs/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/map-sum-pairs/solution/gong-shui-san-xie-jie-he-dfs-de-trie-yun-i4xa/) | 中等 | 🤩🤩🤩🤩 | | [690. 员工的重要性](https://leetcode-cn.com/problems/employee-importance/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/employee-importance/solution/gong-shui-san-xie-yi-ti-shuang-jie-di-gu-s79x/) | 简单 | 🤩🤩🤩 | 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 bf4155e1..26c9418b 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" @@ -4,6 +4,7 @@ | [240. 搜索二维矩阵 II](https://leetcode-cn.com/problems/search-a-2d-matrix-ii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/search-a-2d-matrix-ii/solution/gong-shui-san-xie-yi-ti-shuang-jie-er-fe-y1ns/) | 中等 | 🤩🤩🤩🤩 | | [297. 二叉树的序列化与反序列化](https://leetcode-cn.com/problems/serialize-and-deserialize-binary-tree/) | [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/) | 困难 | 🤩🤩🤩🤩🤩 | | [437. 路径总和 III](https://leetcode-cn.com/problems/path-sum-iii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/path-sum-iii/solution/gong-shui-san-xie-yi-ti-shuang-jie-dfs-q-usa7/) | 中等 | 🤩🤩🤩🤩 | +| [563. 二叉树的坡度](https://leetcode-cn.com/problems/binary-tree-tilt/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/binary-tree-tilt/solution/gong-shui-san-xie-jian-dan-er-cha-shu-di-ekz4/) | 简单 | 🤩🤩🤩🤩 | | [783. 二叉搜索树节点最小距离](https://leetcode-cn.com/problems/minimum-distance-between-bst-nodes/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/minimum-distance-between-bst-nodes/solution/gong-shui-san-xie-yi-ti-san-jie-shu-de-s-7r17/) | 简单 | 🤩🤩🤩 | | [863. 二叉树中所有距离为 K 的结点](https://leetcode-cn.com/problems/all-nodes-distance-k-in-binary-tree/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/all-nodes-distance-k-in-binary-tree/solution/gong-shui-san-xie-yi-ti-shuang-jie-jian-x6hak/) | 中等 | 🤩🤩🤩🤩 | | [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/) | 简单 | 🤩🤩🤩 | diff --git "a/LeetCode/561-570/563. \344\272\214\345\217\211\346\240\221\347\232\204\345\235\241\345\272\246\357\274\210\347\256\200\345\215\225\357\274\211.md" "b/LeetCode/561-570/563. \344\272\214\345\217\211\346\240\221\347\232\204\345\235\241\345\272\246\357\274\210\347\256\200\345\215\225\357\274\211.md" new file mode 100644 index 00000000..9e9f9388 --- /dev/null +++ "b/LeetCode/561-570/563. \344\272\214\345\217\211\346\240\221\347\232\204\345\235\241\345\272\246\357\274\210\347\256\200\345\215\225\357\274\211.md" @@ -0,0 +1,118 @@ +### 题目描述 + +这是 LeetCode 上的 **[563. 二叉树的坡度](https://leetcode-cn.com/problems/binary-tree-tilt/solution/gong-shui-san-xie-jian-dan-er-cha-shu-di-ekz4/)** ,难度为 **简单**。 + +Tag : 「二叉树」、「DFS」 + + + +给定一个二叉树,计算**整个树**的坡度 。 + +一个树的 节点的坡度 定义即为,该节点左子树的节点之和和右子树节点之和的 差的绝对值 。 + +如果没有左子树的话,左子树的节点之和为 $0$ ;没有右子树的话也是一样。空结点的坡度是 $0$ 。 + +整个树 的坡度就是其所有节点的坡度之和。 + +示例 1: +![](https://assets.leetcode.com/uploads/2020/10/20/tilt1.jpg) +``` +输入:root = [1,2,3] + +输出:1 + +解释: +节点 2 的坡度:|0-0| = 0(没有子节点) +节点 3 的坡度:|0-0| = 0(没有子节点) +节点 1 的坡度:|2-3| = 1(左子树就是左子节点,所以和是 2 ;右子树就是右子节点,所以和是 3 ) +坡度总和:0 + 0 + 1 = 1 +``` +示例 2: +![](https://assets.leetcode.com/uploads/2020/10/20/tilt2.jpg) +``` +输入:root = [4,2,9,3,5,null,7] + +输出:15 + +解释: +节点 3 的坡度:|0-0| = 0(没有子节点) +节点 5 的坡度:|0-0| = 0(没有子节点) +节点 7 的坡度:|0-0| = 0(没有子节点) +节点 2 的坡度:|3-5| = 2(左子树就是左子节点,所以和是 3 ;右子树就是右子节点,所以和是 5 ) +节点 9 的坡度:|0-7| = 7(没有左子树,所以和是 0 ;右子树正好是右子节点,所以和是 7 ) +节点 4 的坡度:|(3+5+2)-(9+7)| = |10-16| = 6(左子树值为 3、5 和 2 ,和是 10 ;右子树值为 9 和 7 ,和是 16 ) +坡度总和:0 + 0 + 0 + 2 + 7 + 6 = 15 +``` +示例 3: +![](https://assets.leetcode.com/uploads/2020/10/20/tilt3.jpg) +``` +输入:root = [21,7,14,1,1,2,2,3,3] + +输出:9 +``` + +提示: +* 树中节点数目的范围在 $[0, 10^4]$ 内 +* $-1000 <= Node.val <= 1000$ + +--- + +### 递归 + +根据题目对「坡度」的定义,我们可以直接写出对应的递归实现。 + +代码: +```Java +class Solution { + public int findTilt(TreeNode root) { + if (root == null) return 0; + return findTilt(root.left) + findTilt(root.right) + Math.abs(getSum(root.left) - getSum(root.right)); + } + int getSum(TreeNode root) { + if (root == null) return 0; + return getSum(root.left) + getSum(root.right) + root.val; + } +} +``` +* 时间复杂度:每个节点被访问的次数与其所在深度有关。复杂度为 $O(n^2)$ +* 空间复杂度:忽略递归来带的额外空间消耗。复杂度为 $O(1)$ + +--- + +### 递归 + +上述解法之所以为 $O(n^2)$ 的时间复杂度,是因为我们将「计算子树坡度」和「计算子树权值和」两个操作分开进行。 + +事实上,我们可以在计算子树权值和的时候将坡度进行累加,从而将复杂度降为 $O(n)$。 + +代码: +```Java +class Solution { + int ans; + public int findTilt(TreeNode root) { + dfs(root); + return ans; + } + int dfs(TreeNode root) { + if (root == null) return 0; + int l = dfs(root.left), r = dfs(root.right); + ans += Math.abs(l - r); + return l + r + root.val; + } +} +``` +* 时间复杂度:$O(n)$ +* 空间复杂度:$O(1)$ + +--- + +### 最后 + +这是我们「刷穿 LeetCode」系列文章的第 `No.563` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。 + +在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。 + +为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。 + +在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。 +