Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b72c034

Browse files
Merge pull request SharingSource#229 from SharingSource/ac_oier
✨feat: Add 563
2 parents 92d8deb + b5503df commit b72c034

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed

‎Index/DFS.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
| [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/) | 中等 | 🤩🤩🤩🤩 |
1414
| [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/) | 困难 | 🤩🤩🤩🤩 |
1515
| [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/) | 中等 | 🤩🤩🤩🤩 |
16+
| [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/) | 简单 | 🤩🤩🤩🤩 |
1617
| [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/) | 中等 | 🤩🤩🤩🤩 |
1718
| [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/) | 中等 | 🤩🤩🤩🤩 |
1819
| [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/) | 简单 | 🤩🤩🤩 |

‎Index/二叉树.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
| [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/) | 中等 | 🤩🤩🤩🤩 |
55
| [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/) | 困难 | 🤩🤩🤩🤩🤩 |
66
| [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/) | 中等 | 🤩🤩🤩🤩 |
7+
| [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/) | 简单 | 🤩🤩🤩🤩 |
78
| [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/) | 简单 | 🤩🤩🤩 |
89
| [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/) | 中等 | 🤩🤩🤩🤩 |
910
| [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/) | 简单 | 🤩🤩🤩 |
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[563. 二叉树的坡度](https://leetcode-cn.com/problems/binary-tree-tilt/solution/gong-shui-san-xie-jian-dan-er-cha-shu-di-ekz4/)** ,难度为 **简单**
4+
5+
Tag : 「二叉树」、「DFS」
6+
7+
8+
9+
给定一个二叉树,计算**整个树**的坡度 。
10+
11+
一个树的 节点的坡度 定义即为,该节点左子树的节点之和和右子树节点之和的 差的绝对值 。
12+
13+
如果没有左子树的话,左子树的节点之和为 0ドル$ ;没有右子树的话也是一样。空结点的坡度是 0ドル$ 。
14+
15+
整个树 的坡度就是其所有节点的坡度之和。
16+
17+
示例 1:
18+
![](https://assets.leetcode.com/uploads/2020/10/20/tilt1.jpg)
19+
```
20+
输入:root = [1,2,3]
21+
22+
输出:1
23+
24+
解释:
25+
节点 2 的坡度:|0-0| = 0(没有子节点)
26+
节点 3 的坡度:|0-0| = 0(没有子节点)
27+
节点 1 的坡度:|2-3| = 1(左子树就是左子节点,所以和是 2 ;右子树就是右子节点,所以和是 3 )
28+
坡度总和:0 + 0 + 1 = 1
29+
```
30+
示例 2:
31+
![](https://assets.leetcode.com/uploads/2020/10/20/tilt2.jpg)
32+
```
33+
输入:root = [4,2,9,3,5,null,7]
34+
35+
输出:15
36+
37+
解释:
38+
节点 3 的坡度:|0-0| = 0(没有子节点)
39+
节点 5 的坡度:|0-0| = 0(没有子节点)
40+
节点 7 的坡度:|0-0| = 0(没有子节点)
41+
节点 2 的坡度:|3-5| = 2(左子树就是左子节点,所以和是 3 ;右子树就是右子节点,所以和是 5 )
42+
节点 9 的坡度:|0-7| = 7(没有左子树,所以和是 0 ;右子树正好是右子节点,所以和是 7 )
43+
节点 4 的坡度:|(3+5+2)-(9+7)| = |10-16| = 6(左子树值为 3、5 和 2 ,和是 10 ;右子树值为 9 和 7 ,和是 16 )
44+
坡度总和:0 + 0 + 0 + 2 + 7 + 6 = 15
45+
```
46+
示例 3:
47+
![](https://assets.leetcode.com/uploads/2020/10/20/tilt3.jpg)
48+
```
49+
输入:root = [21,7,14,1,1,2,2,3,3]
50+
51+
输出:9
52+
```
53+
54+
提示:
55+
* 树中节点数目的范围在 $[0, 10^4]$ 内
56+
* $-1000 <= Node.val <= 1000$
57+
58+
---
59+
60+
### 递归
61+
62+
根据题目对「坡度」的定义,我们可以直接写出对应的递归实现。
63+
64+
代码:
65+
```Java
66+
class Solution {
67+
public int findTilt(TreeNode root) {
68+
if (root == null) return 0;
69+
return findTilt(root.left) + findTilt(root.right) + Math.abs(getSum(root.left) - getSum(root.right));
70+
}
71+
int getSum(TreeNode root) {
72+
if (root == null) return 0;
73+
return getSum(root.left) + getSum(root.right) + root.val;
74+
}
75+
}
76+
```
77+
* 时间复杂度:每个节点被访问的次数与其所在深度有关。复杂度为 $O(n^2)$
78+
* 空间复杂度:忽略递归来带的额外空间消耗。复杂度为 $O(1)$
79+
80+
---
81+
82+
### 递归
83+
84+
上述解法之所以为 $O(n^2)$ 的时间复杂度,是因为我们将「计算子树坡度」和「计算子树权值和」两个操作分开进行。
85+
86+
事实上,我们可以在计算子树权值和的时候将坡度进行累加,从而将复杂度降为 $O(n)$。
87+
88+
代码:
89+
```Java
90+
class Solution {
91+
int ans;
92+
public int findTilt(TreeNode root) {
93+
dfs(root);
94+
return ans;
95+
}
96+
int dfs(TreeNode root) {
97+
if (root == null) return 0;
98+
int l = dfs(root.left), r = dfs(root.right);
99+
ans += Math.abs(l - r);
100+
return l + r + root.val;
101+
}
102+
}
103+
```
104+
* 时间复杂度:$O(n)$
105+
* 空间复杂度:$O(1)$
106+
107+
---
108+
109+
### 最后
110+
111+
这是我们「刷穿 LeetCode」系列文章的第 `No.563` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
112+
113+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
114+
115+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
116+
117+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
118+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /