-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryTreeLongestConsecutiveSequence.java
78 lines (69 loc) · 2.38 KB
/
BinaryTreeLongestConsecutiveSequence.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import java.util.ArrayDeque;
import java.util.Queue;
/**
* LeetCode
* 298. Binary Tree Longest Consecutive Sequence
* https://lefttree.gitbooks.io/leetcode-categories/content/BinaryTree/recursion/btLongestConsecutiveSeq.html
* #Medium
*/
public class BinaryTreeLongestConsecutiveSequence {
public static void main(String[] args) {
BinaryTreeLongestConsecutiveSequence sol = new BinaryTreeLongestConsecutiveSequence();
System.out.println(sol.longestConsecutive(buildBstFromLevelOrder(new Integer[]{1, null, 3, 2, 4, null, null, null, 5}))); // 3
System.out.println(sol.longestConsecutive(buildBstFromLevelOrder(new Integer[]{2, null, 3, 2, null, 1, null}))); // 2
}
private int max = 0;
public int longestConsecutive(TreeNode root) {
max = 0;
if (root == null) return 0;
longestConsecutive0(root, root.val, 1);
return max;
}
private void longestConsecutive0(TreeNode node, int val, int len) {
if (node.val == val + 1) {
len++;
} else {
len = 1;
}
max = Math.max(max, len);
if (node.left != null) longestConsecutive0(node.left, node.val, len);
if (node.right != null) longestConsecutive0(node.right, node.val, len);
}
private static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
@Override
public String toString() {
return "{" +
"val=" + val +
", left=" + left +
", right=" + right +
'}';
}
}
private static TreeNode buildBstFromLevelOrder(Integer[] order) {
if (order == null || order.length == 0) return null;
TreeNode head = null;
Queue<TreeNode> parents = new ArrayDeque<>();
TreeNode node;
boolean left = true;
for (Integer val : order) {
node = val == null ? null : new TreeNode(val);
if (head == null) {
head = node;
if (head == null) return null;
}
if (!parents.isEmpty()) {
if (left) parents.peek().left = node;
else parents.poll().right = node;
left = !left;
}
if (node != null) parents.add(node);
}
return head;
}
}