Skip to content

Commit d68d090

Browse files
committed
fd
1 parent 655e51c commit d68d090

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@
391391
|681|[Next Closest Time](https://leetcode.com/problems/next-closest-time/)|[Java](leetcode/solution/src/NextClosestTime.java)|80|这题多做几遍|
392392
|683|[K Empty Slots](https://leetcode.com/problems/k-empty-slots/)|[Java](leetcode/solution/src/KEmptySlots.java)|80||
393393
|684|[Redundant Connection](https://leetcode.com/problems/redundant-connection/)| [Java](leetcode/solution/src/RedundantConnection.java)|||
394+
|687|[Longest Univalue Path](https://leetcode.com/problems/longest-univalue-path/)|[Java](leetcode/solution/src/longestUnivaluePath.java)|70||
394395
|695|[Max Area of Island](https://leetcode.com/problems/max-area-of-island/)|[Java](leetcode/solution/src/MaxAreaOfIsland.java)|100||
395396
|703|[Kth Largest Element in a Stream](https://leetcode.com/problems/kth-largest-element-in-a-stream/)|[Java](leetcode/solution/src/KthLargestElementInAStream.java)|100||
396397
|771|[Jewels and Stones](https://leetcode.com/problems/jewels-and-stones/)|[Java](leetcode/solution/src/JewelsAndStones.java)|100||

leetcode/solution/src/CountUnivalueSubtrees.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
public class CountUnivalueSubtrees {
22

3+
/**
4+
* 先分别判断左右子树是不是unival,如果是再判断带上root后是不是unival
5+
*/
36
public int countUnivalSubtrees(TreeNode root) {
47
int[] count = new int[1];
58
helper(root, count);

leetcode/src/Main.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,29 @@ public class Main {
66

77
public static class Solution {
88

9-
public List<TreeNode> generateTrees(int n) {
9+
public int longestUnivaluePath(TreeNode root) {
10+
int[] res = new int[1];
11+
dfs(root, res);
12+
return res[0];
13+
}
14+
15+
private int dfs(TreeNode node, int[] res) {
16+
if (node == null) {
17+
return 0;
18+
}
19+
int left = dfs(node.left, res);
20+
int right = dfs(node.right, res);
21+
22+
int lval = 0, rval = 0;
1023

24+
if (node.left != null && node.left.val == node.val) {
25+
lval = left + 1;
26+
}
27+
if (node.right != null && node.right.val == node.val) {
28+
rval = right + 1;
29+
}
30+
res[0] = Math.max(res[0], lval + rval);
31+
return Math.max(lval, rval);
1132
}
1233
}
1334

0 commit comments

Comments
 (0)