-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
44 lines (38 loc) · 1.49 KB
/
Solution.java
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int longestUnivaluePath(TreeNode root) {
int longestPath = 0;
Map<TreeNode, Integer> pathMap = new HashMap<TreeNode, Integer>();
LinkedList<TreeNode> stack = new LinkedList<TreeNode>();
if (root != null) stack.addLast(root);
while (stack.size() > 0) {
TreeNode node = stack.getLast();
if (node.left != null && pathMap.get(node.left) == null) {
stack.addLast(node.left);
} else if (node.right != null && pathMap.get(node.right) == null) {
stack.addLast(node.right);
} else {
stack.removeLast();
int path = 0;
if (node.left != null || node.right != null) {
int leftPath = node.left != null && node.left.val == node.val ? 1 + pathMap.get(node.left) : 0,
rightPath = node.right != null && node.right.val == node.val ? 1 + pathMap.get(node.right) : 0;
pathMap.put(node, Math.max(leftPath, rightPath));
path = leftPath + rightPath;
} else {
pathMap.put(node, 0);
}
longestPath = Math.max(longestPath, path);
}
}
return longestPath;
}
}