-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha_687.java
More file actions
27 lines (26 loc) · 801 Bytes
/
a_687.java
File metadata and controls
27 lines (26 loc) · 801 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package datastructure.tree;
/**
* 相同节点值的最大路径长度, 最长同值路径
*/
public class a_687 {
private int path;
public int longestUnivaluePath(TreeNode root) {
dfs(root);
return path;
}
private int dfs(TreeNode root) {
if (root == null) return 0;
int left = dfs(root.left);
int right = dfs(root.right);
int leftPath = ( root.left != null && root.left.val == root.val ) ? left + 1 : 0;
int rightPath = ( root.right != null && root.right.val == root.val) ? right + 1 : 0;
path = Math.max(leftPath + rightPath, path);
return Math.max(leftPath, rightPath);
}
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
}