Skip to content

Commit f9232fb

Browse files
committed
feat: add the solution of Longest Univalue Path(687) with Java.
1 parent 98ab4a2 commit f9232fb

File tree

3 files changed

+142
-21
lines changed

3 files changed

+142
-21
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
| [680][680-question] | [Valid Palindrome II][680-tips] | [][680-java] | [][680-js] | |
7575
| [682][682-question] | [Baseball Game][682-tips] | [][682-java] | [][682-js] | |
7676
| [686][686-question] | [Repeated String Match][686-tips] | [][686-java] | [][686-js] | |
77-
| [687][687-question] | [Longest Univalue Path][687-tips] | | [][687-js] | |
77+
| [687][687-question] | [Longest Univalue Path][687-tips] | [][687-java] | [][687-js] | |
7878
| [693][693-question] | [Binary Number with Alternating Bits][693-tips] | | [][693-js] | |
7979
| [695][695-question] | [Max Area of Island][695-tips] | | [][695-js] | |
8080
| [696][696-question] | [Count Binary Substrings][696-tips] | | [][696-js] | |
@@ -471,6 +471,7 @@
471471
[674-java]: ./src/_674/Solution.java
472472
[680-java]: ./src/_680/Solution.java
473473
[686-java]: ./src/_686/Solution.java
474+
[687-java]: ./src/_687/Solution.java
474475
[682-java]: ./src/_682/Solution.java
475476
[728-java]: ./src/_728/Solution.java
476477
[771-java]: ./src/_771/Solution.java

src/_687/Solution.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package _687;
2+
3+
class Solution {
4+
private int maxLen = 0;
5+
6+
public int longestUnivaluePath(TreeNode root) {
7+
if (root == null) {
8+
return 0;
9+
}
10+
dfs(root, root.val + 1);
11+
return maxLen;
12+
}
13+
14+
private int dfs(TreeNode node, int rootVal) {
15+
if (node == null) return 0;
16+
17+
int left = dfs(node.left, node.val);
18+
int right = dfs(node.right, node.val);
19+
20+
if (node.val == rootVal) {
21+
return Math.max(left, right) + 1;
22+
}
23+
24+
maxLen = Math.max(maxLen, left + right);
25+
26+
return 0;
27+
}
28+
29+
private class TreeNode {
30+
int val;
31+
TreeNode left;
32+
TreeNode right;
33+
34+
TreeNode(int x) {
35+
val = x;
36+
}
37+
}
38+
}

tips/687/README.md

Lines changed: 102 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,128 @@
1-
[xxxx][title]
1+
[Longest Univalue Path][title]
22

33
## Description
4-
// 抄题目
54

5+
Given a binary tree, find the length of the longest path where each node in the path has the same value. This path may or may not pass through the root.
66

7-
**Example:**
7+
**Note:** The length of path between two nodes is represented by the number of edges between them.
8+
9+
**Example 1:**
10+
11+
Input:
812

913
```
10-
// 抄Example
14+
5
15+
/ \
16+
4 5
17+
/ \ \
18+
1 1 5
1119
```
1220

13-
**Note:**
14-
// Note
21+
Output:
1522

16-
**Tags:** // tags
23+
```
24+
2
25+
```
1726

27+
**Example 2:**
1828

19-
## 思路 1
20-
// 贴一些关键代码,说一些解题思路
21-
// (同一种语言可以写多种思路,与某种语言思路相同的另一种语言的思路无须赘述,但可以把代码贴在后面)
22-
```java
29+
Input:
2330

2431
```
25-
```javascript
32+
1
33+
/ \
34+
4 5
35+
/ \ \
36+
4 4 5
37+
```
38+
39+
Output:
2640

41+
```
42+
2
2743
```
2844

29-
## 思路 2
30-
// 贴一些关键代码,说一些解题思路
31-
```java
45+
**Note:** The given binary tree has not more than 10000 nodes. The height of the tree is not more than 1000.
3246

47+
**Tags:** [Tree](https://leetcode.com/tag/tree/)[Recursion](https://leetcode.com/tag/recursion/)
48+
49+
50+
## 思路
51+
52+
计算二叉树中最长的连续相同节点所形成的的路径的长度(节点之间边的数量代表长度)。根据二叉树的特性,用深度优先搜索,对连续相同的节点进行计数即可,需要遵循以下原则:
53+
54+
- 当遇到两条相同的路径可以连接至同一个父节点时,选择较长的路径即可。例如:
55+
56+
```
57+
1
58+
/ \
59+
[1] 2
60+
/ \
61+
1 1
62+
/
63+
1
3364
```
3465

35-
## 思路 3
36-
// 贴一些关键代码,说一些解题思路
37-
```kotlin
66+
在所框选的节点上有两条路径连接着。即:
3867

68+
```
69+
1 1
70+
/ /
71+
[1] [1]
72+
/ 或 \
73+
1 1
74+
/
75+
1
76+
```
77+
78+
当然,是选择左边的这条较长的路径。
79+
80+
- 当遇到两条相同的路径可以连接至同一个父节点,且此父节点不能继续向上连接时,可以将两个子节点路径连接起来。例如:
81+
82+
```
83+
[1]
84+
/ \
85+
1 1
86+
/
87+
1
88+
```
89+
90+
此时的路径长度等于左边的两条加上右边的一条,总长度为三。
91+
92+
**Java:**
93+
94+
```java
95+
class Solution {
96+
private int maxLen = 0;
97+
98+
public int longestUnivaluePath(TreeNode root) {
99+
if (root == null) {
100+
return 0;
101+
}
102+
dfs(root, root.val + 1);
103+
return maxLen;
104+
}
105+
106+
private int dfs(TreeNode node, int rootVal) {
107+
if (node == null) return 0;
108+
109+
int left = dfs(node.left, node.val);
110+
int right = dfs(node.right, node.val);
111+
112+
if (node.val == rootVal) {
113+
return Math.max(left, right) + 1;
114+
}
115+
116+
maxLen = Math.max(maxLen, left + right);
117+
118+
return 0;
119+
}
120+
}
39121
```
40122

41123
## 结语
42-
124+
43125
如果你同我们一样热爱数据结构、算法、LeetCode,可以关注我们 GitHub 上的 LeetCode 题解:[LeetCode-Solution][ls]
44126

45-
[title]: https://leetcode.com/problems/xxxx
127+
[title]: https://leetcode.com/problems/longest-univalue-path/description/
46128
[ls]: https://github.com/RichCodersAndMe/LeetCode-Solution

0 commit comments

Comments
 (0)