Skip to content

Commit 751dc9f

Browse files
committed
BinaryTreeLongestConsecutiveSequence298
1 parent 3548401 commit 751dc9f

File tree

2 files changed

+108
-2
lines changed

2 files changed

+108
-2
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@
4444
| [Graph](https://github.com/fluency03/leetcode-java/blob/master/src/graph) |
4545

4646

47-
# Total: 379
47+
# Total: 380
4848

4949
| Easy | Medium | Hard | - |
5050
|:-------:|:-------:|:----:|:-:|
51-
| 102 | 207 | 66 | 4 |
51+
| 102 | 208 | 66 | 4 |
5252

5353

5454
| Question | Solution | Difficulty |
@@ -259,6 +259,7 @@
259259
| [289. Game of Life](https://leetcode.com/problems/game-of-life/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/GameOfLife289.java) | Medium |
260260
| [295. Find Median from Data Stream](https://leetcode.com/problems/find-median-from-data-stream/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/FindMedianFromDataStream295.java) | Hard |
261261
| [297. Serialize and Deserialize Binary Tree](https://leetcode.com/problems/serialize-and-deserialize-binary-tree/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/SerializeAndDeserializeBinaryTree297.java) | Hard |
262+
| [298. Binary Tree Longest Consecutive Sequence](https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/BinaryTreeLongestConsecutiveSequence298.java) | Medium |
262263
| [299. Bulls and Cows](https://leetcode.com/problems/bulls-and-cows/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/BullsAndCows299.java) | Medium |
263264
| [300. Longest Increasing Subsequence](https://leetcode.com/problems/longest-increasing-subsequence/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/LongestIncreasingSubsequence300.java) | Medium |
264265
| [301. Remove Invalid Parentheses](https://leetcode.com/problems/remove-invalid-parentheses/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/RemoveInvalidParentheses301.java) | Hard |
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/**
2+
* Given a binary tree, find the length of the longest consecutive sequence path.
3+
*
4+
* The path refers to any sequence of nodes from some starting node to any node
5+
* in the tree along the parent-child connections. The longest consecutive path
6+
* need to be from parent to child (cannot be the reverse).
7+
*
8+
* Example 1:
9+
*
10+
* Input:
11+
*
12+
* 1
13+
* \
14+
* 3
15+
* / \
16+
* 2 4
17+
* \
18+
* 5
19+
*
20+
* Output: 3
21+
* Explanation: Longest consecutive sequence path is 3-4-5, so return 3.
22+
*
23+
* Example 2:
24+
*
25+
* Input:
26+
*
27+
* 2
28+
* \
29+
* 3
30+
* /
31+
* 2
32+
* /
33+
* 1
34+
*
35+
* Output: 2
36+
* Explanation: Longest consecutive sequence path is 2-3, not 3-2-1, so return 2.
37+
*/
38+
39+
/**
40+
* Definition for a binary tree node.
41+
* public class TreeNode {
42+
* int val;
43+
* TreeNode left;
44+
* TreeNode right;
45+
* TreeNode(int x) { val = x; }
46+
* }
47+
*/
48+
49+
public class BinaryTreeLongestConsecutiveSequence298 {
50+
public int longestConsecutive(TreeNode root) {
51+
if (root == null) return 0;
52+
return helper(root, 0);
53+
}
54+
55+
public int helper(TreeNode root, int i) {
56+
if (root == null) return i;
57+
58+
int l = i + 1;
59+
if (root.left != null) {
60+
if (root.val + 1 == root.left.val) {
61+
l = helper(root.left, i+1);
62+
} else {
63+
l = helper(root.left, 0);
64+
}
65+
}
66+
67+
int r = i + 1;
68+
if (root.right != null) {
69+
if (root.val + 1 == root.right.val) {
70+
r = helper(root.right, i+1);
71+
} else {
72+
r = helper(root.right, 0);
73+
}
74+
}
75+
76+
return Math.max(Math.max(l, r), i+1);
77+
}
78+
79+
80+
/**
81+
* Bottom-up
82+
* https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/solution/
83+
*/
84+
private int maxLength = 0;
85+
public int longestConsecutive2(TreeNode root) {
86+
dfs(root);
87+
return maxLength;
88+
}
89+
90+
private int dfs(TreeNode p) {
91+
if (p == null) return 0;
92+
int L = dfs(p.left) + 1;
93+
int R = dfs(p.right) + 1;
94+
if (p.left != null && p.val + 1 != p.left.val) {
95+
L = 1;
96+
}
97+
if (p.right != null && p.val + 1 != p.right.val) {
98+
R = 1;
99+
}
100+
int length = Math.max(L, R);
101+
maxLength = Math.max(maxLength, length);
102+
return length;
103+
}
104+
105+
}

0 commit comments

Comments
 (0)