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 c3ae70f

Browse files
Add Solution2.java to problems 0687
1 parent 04b5334 commit c3ae70f

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
11+
class Solution {
12+
13+
private int maxL = 0;
14+
15+
public int longestUnivaluePath(TreeNode root) {
16+
if(root == null) return 0;
17+
getMaxL(root, root.val);
18+
return maxL;
19+
}
20+
21+
private int getMaxL(TreeNode r, int val) {
22+
if (r == null) return 0;
23+
int left = getMaxL(r.left, r.val);
24+
int right = getMaxL(r.right, r.val);
25+
maxL = Math.max(maxL, left + right);
26+
return r.val == val ? Math.max(left, right) + 1 : 0;
27+
}
28+
}

0 commit comments

Comments
(0)

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