Skip to content

Commit 5fa4ede

Browse files
authored
Create Solution.java
1 parent acf82df commit 5fa4ede

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public int longestUnivaluePath(TreeNode root) {
3+
int[] res = new int[1];
4+
dfs(root, res);
5+
return res[0];
6+
}
7+
8+
private int dfs(TreeNode root, int[] res) {
9+
if (root == null) {
10+
return 0;
11+
}
12+
int left = dfs(root.left, res);
13+
int right = dfs(root.right, res);
14+
left = root.left != null && root.left.val == root.val ? left + 1 : 0;
15+
right = root.right != null && root.right.val == root.val ? right + 1 : 0;
16+
res[0] = Math.max(res[0], left + right);
17+
return Math.max(left, right);
18+
}
19+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy