|
| 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 | + |
| 15 | +``` |
| 16 | +输入:root = [5,4,5,1,1,5] |
| 17 | +
|
| 18 | +输出:2 |
| 19 | +``` |
| 20 | +示例 2: |
| 21 | + |
| 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