Skip to content

Commit 3ef6811

Browse files
committed
✨feat: Add 687
1 parent 76c8942 commit 3ef6811

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

Index/DFS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
| [662. 二叉树最大宽度](https://leetcode.cn/problems/maximum-width-of-binary-tree/) | [LeetCode 题解链接](https://leetcode.cn/problems/maximum-width-of-binary-tree/solution/by-ac_oier-33er/) | 中等 | 🤩🤩🤩🤩 |
3838
| [676. 实现一个魔法字典](https://leetcode.cn/problems/implement-magic-dictionary/) | [LeetCode 题解链接](https://leetcode.cn/problems/implement-magic-dictionary/solution/by-ac_oier-a01l/) | 中等 | 🤩🤩🤩🤩🤩 |
3939
| [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/) | 中等 | 🤩🤩🤩🤩 |
40+
| [687. 最长同值路径](https://leetcode.cn/problems/longest-univalue-path/) | [LeetCode 题解链接](https://leetcode.cn/problems/longest-univalue-path/solution/by-ac_oier-8ue8/) | 中等 | 🤩🤩🤩🤩 |
4041
| [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/) | 简单 | 🤩🤩🤩 |
4142
| [691. 贴纸拼词](https://leetcode.cn/problems/stickers-to-spell-word/) | [LeetCode 题解链接](https://leetcode.cn/problems/stickers-to-spell-word/solution/by-ac_oier-5vv3/) | 困难 | 🤩🤩🤩🤩 |
4243
| [736. Lisp 语法解析](https://leetcode.cn/problems/parse-lisp-expression/) | [LeetCode 题解链接](https://leetcode.cn/problems/parse-lisp-expression/solution/by-ac_oier-i7w1/) | 困难 | 🤩🤩🤩🤩 |

Index/二叉树.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
| [654. 最大二叉树](https://leetcode.cn/problems/maximum-binary-tree/) | [LeetCode 题解链接](https://leetcode.cn/problems/maximum-binary-tree/solution/by-ac_oier-s0wc/) | 中等 | 🤩🤩🤩🤩🤩 |
1414
| [655. 输出二叉树](https://leetcode.cn/problems/print-binary-tree/) | [LeetCode 题解链接](https://leetcode.cn/problems/print-binary-tree/solution/by-ac_oier-mays/) | 中等 | 🤩🤩🤩🤩 |
1515
| [662. 二叉树最大宽度](https://leetcode.cn/problems/maximum-width-of-binary-tree/) | [LeetCode 题解链接](https://leetcode.cn/problems/maximum-width-of-binary-tree/solution/by-ac_oier-33er/) | 中等 | 🤩🤩🤩🤩🤩 |
16+
| [687. 最长同值路径](https://leetcode.cn/problems/longest-univalue-path/) | [LeetCode 题解链接](https://leetcode.cn/problems/longest-univalue-path/solution/by-ac_oier-8ue8/) | 中等 | 🤩🤩🤩🤩 |
1617
| [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/) | 简单 | 🤩🤩🤩 |
1718
| [814. 二叉树剪枝](https://leetcode.cn/problems/binary-tree-pruning/) | [LeetCode 题解链接](https://leetcode.cn/problems/binary-tree-pruning/solution/by-ac_oier-7me9/) | 中等 | 🤩🤩🤩🤩🤩 |
1819
| [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/) | 中等 | 🤩🤩🤩🤩 |
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[687. 最长同值路径](https://leetcode.cn/problems/longest-univalue-path/solution/by-ac_oier-8ue8/)** ,难度为 **中等**
4+
5+
Tag : 「二叉树」、「DFS」、「递归」
6+
7+
8+
9+
给定一个二叉树的 `root`,返回 最长的路径的长度 ,这个路径中的 每个节点具有相同值 。 这条路径可以经过也可以不经过根节点。
10+
11+
两个节点之间的路径长度 由它们之间的边数表示。
12+
13+
示例 1:
14+
![](https://assets.leetcode.com/uploads/2020/10/13/ex1.jpg)
15+
```
16+
输入:root = [5,4,5,1,1,5]
17+
18+
输出:2
19+
```
20+
示例 2:
21+
![](https://assets.leetcode.com/uploads/2020/10/13/ex2.jpg)
22+
```
23+
输入:root = [1,4,5,4,4,5]
24+
25+
输出:2
26+
```
27+
28+
提示:
29+
* 树的节点数的范围是 $[0, 10^4] $
30+
* $-1000 <= Node.val <= 1000$
31+
* 树的深度将不超过 `1000` 
32+
33+
---
34+
35+
### 递归
36+
37+
设计递归函数 `int dfs(TreeNode root)`,含义为传入根节点 `root`,返回以该节点为起点,往下走同值路径所能经过的最大路径长度(即不能同时往左右节点走),同时使用全局变量 `max` 记录答案路径所能经过最大路径长度。
38+
39+
在递归函数内部,先通过递归 `root` 的左右子节点,拿到以 `root.left``root.right` 为起点的最大路径长度 `l``r`,然后根据当前节点值和左右子节点值的相等关系来更新 `ans`,同时用 `cur` 维护「以当前节点 `root` 为目标路径中深度最小(位置最高)节点时」所经过的最大路径长度。
40+
41+
Java 代码:
42+
```Java
43+
class Solution {
44+
int max = 0;
45+
public int longestUnivaluePath(TreeNode root) {
46+
dfs(root);
47+
return max;
48+
}
49+
int dfs(TreeNode root) {
50+
if (root == null) return 0;
51+
int ans = 0, cur = 0, l = dfs(root.left), r = dfs(root.right);
52+
if (root.left != null && root.left.val == root.val) {
53+
ans = l + 1; cur += l + 1;
54+
}
55+
if (root.right != null && root.right.val == root.val) {
56+
ans = Math.max(ans, r + 1); cur += r + 1;
57+
}
58+
max = Math.max(max, cur);
59+
return ans;
60+
}
61+
}
62+
```
63+
TypeScript 代码:
64+
```TypeScript
65+
let max = 0;
66+
function longestUnivaluePath(root: TreeNode | null): number {
67+
max = 0
68+
dfs(root)
69+
return max
70+
};
71+
function dfs(root: TreeNode | null): number {
72+
if (root == null) return 0
73+
let ans = 0, cur = 0, l = dfs(root.left), r = dfs(root.right)
74+
if (root.left != null && root.left.val == root.val) {
75+
ans = l + 1; cur += l + 1
76+
}
77+
if (root.right != null && root.right.val == root.val) {
78+
ans = Math.max(ans, r + 1); cur += r + 1
79+
}
80+
max = Math.max(max, cur)
81+
return ans
82+
}
83+
```
84+
* 时间复杂度:$O(n)$
85+
* 空间复杂度:忽略递归带来的额外空间开销,复杂度为 $O(1)$
86+
87+
---
88+
89+
### 最后
90+
91+
这是我们「刷穿 LeetCode」系列文章的第 `No.687` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
92+
93+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
94+
95+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
96+
97+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
98+

0 commit comments

Comments
 (0)