Skip to content

Commit a036a9a

Browse files
committed
add 298 java, progress
1 parent 91e27b2 commit a036a9a

File tree

6 files changed

+297
-6
lines changed

6 files changed

+297
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,7 @@
836836
129| [Sum Root to Leaf Numbers](https://leetcode.com/problems/sum-root-to-leaf-numbers/) |[Python](./leetcode_python/Recursion/sum-root-to-leaf-numbers.py) | _O(n)_ | _O(h)_ | Medium |`trick`,`BST`, `dfs`, `fb`| AGAIN*** (4)
837837
156| [Binary Tree Upside Down](https://leetcode.com/problems/binary-tree-upside-down/) | [Python](./leetcode_python/Recursion/binary-tree-upside-down.py) | _O(n)_ | _O(1)_ | Medium |🔒| AGAIN (not start)
838838
241| [Different Ways to Add Parentheses](https://leetcode.com/problems/different-ways-to-add-parentheses/) | [Python](./leetcode_python/Recursion/different-ways-to-add-parentheses.py) | _O(n * 4^n / n^(3/2))_ | _O(n * 4^n / n^(3/2))_ | Medium || AGAIN (not start)
839-
298 | [Binary Tree Longest Consecutive Sequence](https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/) | [Python](./leetcode_python/Recursion/binary-tree-longest-consecutive-sequence.py) | _O(n)_ | _O(h)_ | Medium |bfs, dfs, tree, 🔒, `google`| OK***** (3)
839+
298 | [Binary Tree Longest Consecutive Sequence](https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/) | [Python](./leetcode_python/Recursion/binary-tree-longest-consecutive-sequence.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Recursion/BinaryTreeLongestConsecutiveSequence.java) | _O(n)_ | _O(h)_ | Medium |good basic, bfs, dfs, tree, 🔒, `google`| OK***** (3)
840840
333 | [Largest BST Subtree](https://leetcode.com/problems/largest-bst-subtree/) | [Python](./leetcode_python/Recursion/largest-bst-subtree.py) | _O(n)_ | _O(h)_ | Medium |🔒| AGAIN (not start)
841841
337| [House Robber III](https://leetcode.com/problems/house-robber-iii/) | [Python](./leetcode_python/Recursion/house-robber-iii.py) | _O(n)_ _O(h)_| Medium|amazon, google| AGAIN (not start)
842842
395| [Longest Substring with At Least K Repeating Characters](https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/) |[Python](./leetcode_python/Recursion/longest-substring-with-at-least-k-repeating-characters.py) | _O(n)_ | _O(1)_ | Medium |`AGAIN`,`recursion`, `good trick`,`fb`| AGAIN************ (4)

data/progress.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Progress
22

3+
# 2024-10-19
4+
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf
5+
36
# 2024-10-18
47
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf
58

data/progress.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
20241019: 298
12
20241018: 1146
23
20241014: 737
34
20241013: 686,734,737

data/to_review.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -832,19 +832,19 @@
832832
2020-07-10 -> ['734,737', '388', '836,860,863']
833833
2020-07-09 -> ['066,271', '694']
834834
2020-07-08 -> ['066,271', '163', '646', '663']
835-
2020-07-07 -> ['066,271', '210,261', '298', '776', '661,662', '703,787,819']
835+
2020-07-07 -> ['298', '066,271', '210,261', '776', '661,662', '703,787,819']
836836
2020-07-06 -> ['130', '163', '361,393,133,207', '669,682,739,763']
837837
2020-07-05 -> ['163', '734,737', '346,686', '771,775', '701,450', '642,652,657']
838838
2020-07-04 -> ['163', '210,261', '640,645', '545,617,628']
839839
2020-07-03 -> ['361,393,133,207', '482,127,102,107', '762', '606,459']
840-
2020-07-02 -> ['210,261', '734,737', '298', '388', '742', '445,508']
840+
2020-07-02 -> ['298', '210,261', '734,737', '388', '742', '445,508']
841841
2020-07-01 -> ['130', '269', '210,261', '361,393,133,207', '322,380,394']
842842
2020-06-30 -> ['210,261', '361,393,133,207', '734,737', '346,686', '646', '738', '537,553,579', '289,295']
843-
2020-06-29 -> ['361,393,133,207', '734,737', '298', '776', '396', '251,287']
843+
2020-06-29 -> ['298', '361,393,133,207', '734,737', '776', '396', '251,287']
844844
2020-06-28 -> ['130', '734,737', '482,127,102,107', '355,119,536', '231,232,240']
845845
2020-06-27 -> ['298', '346,686', '388', '771,775', '451,414,189', '198,203,230']
846-
2020-06-26 -> ['130', '298', '694', '155,165,167,179']
847-
2020-06-25 -> ['130', '298', '346,686', '482,127,102,107', '646', '762', '663', '725,538,089']
846+
2020-06-26 -> ['298', '130', '694', '155,165,167,179']
847+
2020-06-25 -> ['298', '130', '346,686', '482,127,102,107', '646', '762', '663', '725,538,089']
848848
2020-06-24 -> ['130', '346,686', '388', '776', '742', '661,662', '517,535,529', '153']
849849
2020-06-23 -> ['346,686', '482,127,102,107', '711,046,126,127', '131,134,150']
850850
2020-06-22 -> ['482,127,102,107', '388', '646', '771,775', '738', '701,450', '063,064,120,0146']
Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
package LeetCodeJava.Recursion;
2+
3+
// https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/description/
4+
// https://leetcode.ca/all/298.html
5+
6+
import LeetCodeJava.DataStructure.TreeNode;
7+
8+
import java.util.LinkedList;
9+
import java.util.Queue;
10+
11+
/**
12+
*
13+
* 298. Binary Tree Longest Consecutive Sequence
14+
* Given a binary tree, find the length of the longest consecutive sequence path.
15+
*
16+
* The path refers to any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The longest consecutive path need to be from parent to child (cannot be the reverse).
17+
*
18+
* Example 1:
19+
*
20+
* Input:
21+
*
22+
* 1
23+
* \
24+
* 3
25+
* / \
26+
* 2 4
27+
* \
28+
* 5
29+
*
30+
* Output: 3
31+
*
32+
* Explanation: Longest consecutive sequence path is 3-4-5, so return 3.
33+
* Example 2:
34+
*
35+
* Input:
36+
*
37+
* 2
38+
* \
39+
* 3
40+
* /
41+
* 2
42+
* /
43+
* 1
44+
*
45+
* Output: 2
46+
*
47+
* Explanation: Longest consecutive sequence path is 2-3, not 3-2-1, so return 2.
48+
* Difficulty:
49+
* Medium
50+
* Lock:
51+
* Prime
52+
* Company:
53+
* Amazon Facebook Google Uber
54+
*
55+
*/
56+
public class BinaryTreeLongestConsecutiveSequence {
57+
58+
/**
59+
* Definition for a binary tree node.
60+
* public class TreeNode {
61+
* int val;
62+
* TreeNode left;
63+
* TreeNode right;
64+
* TreeNode() {}
65+
* TreeNode(int val) { this.val = val; }
66+
* TreeNode(int val, TreeNode left, TreeNode right) {
67+
* this.val = val;
68+
* this.left = left;
69+
* this.right = right;
70+
* }
71+
* }
72+
*/
73+
// V0
74+
// IDEA : DFS + BFS (fix by gpt)
75+
// TODO : validate it
76+
int longestCnt = 0;
77+
78+
public int longestConsecutive_0_1(TreeNode root) {
79+
if (root == null) {
80+
return 0;
81+
}
82+
83+
// bfs
84+
Queue<TreeNode> q = new LinkedList<>();
85+
q.add(root);
86+
87+
while (!q.isEmpty()) {
88+
TreeNode node = q.poll(); // Poll the node from the queue
89+
int length = dfsCount(node); // Use the node for dfs
90+
longestCnt = Math.max(longestCnt, length);
91+
92+
if (node.left != null) {
93+
q.add(node.left);
94+
}
95+
if (node.right != null) {
96+
q.add(node.right);
97+
}
98+
}
99+
100+
return longestCnt;
101+
}
102+
103+
private int dfsCount(TreeNode root) {
104+
if (root == null) {
105+
return 0;
106+
}
107+
108+
int leftCount = 0, rightCount = 0;
109+
110+
if (root.left != null && root.val + 1 == root.left.val) {
111+
leftCount = 1 + dfsCount(root.left); // Count consecutive path on the left
112+
}
113+
114+
if (root.right != null && root.val + 1 == root.right.val) {
115+
rightCount = 1 + dfsCount(root.right); // Count consecutive path on the right
116+
}
117+
118+
/**
119+
* NOTE !!!
120+
*
121+
* simply compare leftCount and rightCount,
122+
* no need to do below :
123+
*
124+
*
125+
* return Math.max(
126+
* Math.max(this.dfsCount(root.left), this.dfsCount(root.right)),
127+
* this.dfsCount(root)
128+
* );
129+
*
130+
* above code may have issue below :
131+
*
132+
* 1. Redundant Call: this.dfsCount(root) is included in the comparison with dfsCount(root.left) and dfsCount(root.right), leading to redundant recursion. This will likely cause an infinite loop or unnecessary repeated calculations since dfsCount(root) will keep calling itself.
133+
* 2. Logic Error: The comparison includes the root itself in a way that doesn’t make sense in the context of a consecutive sequence. You want to evaluate the left and right children, not compare the root node against itself.
134+
* 3. Performance Degradation: Since this.dfsCount(root) is unnecessarily called multiple times, it can dramatically increase the recursion depth and the overall computational complexity.
135+
*
136+
*/
137+
// Return the max length between left and right consecutive paths
138+
return Math.max(leftCount, rightCount) + 1; // Include current node
139+
}
140+
141+
// V1
142+
// IDEA : TREE, RECURSION
143+
// https://leetcode.ca/2016-09-23-298-Binary-Tree-Longest-Consecutive-Sequence/
144+
private int ans;
145+
146+
public int longestConsecutive_1_1(TreeNode root) {
147+
dfs_1(root);
148+
return ans;
149+
}
150+
151+
private int dfs_1(TreeNode root) {
152+
if (root == null) {
153+
return 0;
154+
}
155+
int l = dfs_1(root.left) + 1;
156+
int r = dfs_1(root.right) + 1;
157+
if (root.left != null && root.left.val - root.val != 1) {
158+
l = 1;
159+
}
160+
if (root.right != null && root.right.val - root.val != 1) {
161+
r = 1;
162+
}
163+
int t = Math.max(l, r);
164+
ans = Math.max(ans, t);
165+
return t;
166+
}
167+
168+
// V2
169+
// IDEA : DFS
170+
// https://walkccc.me/LeetCode/problems/298/#__tabbed_1_2
171+
public int longestConsecutive_2_1(TreeNode root) {
172+
if (root == null)
173+
return 0;
174+
return dfs_2(root, -1, 0, 1);
175+
}
176+
177+
private int dfs_2(TreeNode root, int target, int length, int mx) {
178+
if (root == null)
179+
return mx;
180+
if (root.val == target)
181+
mx = Math.max(mx, ++length);
182+
else
183+
length = 1;
184+
return Math.max(dfs_2(root.left, root.val + 1, length, mx),
185+
dfs_2(root.right, root.val + 1, length, mx));
186+
}
187+
188+
// V3
189+
// IDEA : DFS
190+
// https://blog.csdn.net/qq_46105170/article/details/108481001
191+
private int res;
192+
193+
public int longestConsecutive_3(TreeNode root) {
194+
dfs_3(root);
195+
return res;
196+
}
197+
198+
// The dfs function starts at the root and returns the length of the longest consecutive sequence
199+
private int dfs_3(TreeNode root) {
200+
if (root == null) {
201+
return 0;
202+
}
203+
204+
// Initialize the length to 1
205+
int len = 1;
206+
207+
// Get the longest consecutive path from left and right child nodes
208+
int left = dfs_3(root.left), right = dfs_3(root.right);
209+
210+
// If the left child is consecutive, increase the length
211+
if (root.left != null && root.left.val == root.val + 1) {
212+
len = Math.max(len, 1 + left);
213+
}
214+
215+
// If the right child is consecutive, increase the length
216+
if (root.right != null && root.right.val == root.val + 1) {
217+
len = Math.max(len, 1 + right);
218+
}
219+
220+
// Update the global result
221+
res = Math.max(res, len);
222+
223+
// Return the length of the longest consecutive path starting from the current node
224+
return len;
225+
}
226+
227+
}

leetcode_java/src/main/java/dev/workspace5.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1554,6 +1554,66 @@ public int get(int index, int snap_id) {
15541554
// }
15551555
// }
15561556

1557+
// LC 298
1558+
/// https://leetcode.ca/all/298.html
1559+
// 2.33 pm - 3.00 pm
1560+
/**
1561+
* A consecutive sequence path is a
1562+
* path where the values increase by one along the path.
1563+
*
1564+
* -> start from each nodes, get longest path
1565+
* -> compare with current longest path
1566+
* -> return final ans
1567+
*
1568+
*/
1569+
int longestCnt = 0;
1570+
public int longestConsecutive_1_1(TreeNode root) {
1571+
if (root.left == null && root.right == null){
1572+
return 0;
1573+
}
1574+
1575+
// bfs
1576+
Queue<TreeNode> q = new LinkedList<>();
1577+
q.add(root);
1578+
while (!q.isEmpty()){
1579+
TreeNode node = q.poll();
1580+
// dfs
1581+
int length = dfsCount(node);
1582+
longestCnt = Math.max(longestCnt, length);
1583+
if (node.left != null){
1584+
q.add(node.left);
1585+
}
1586+
if (node.right != null){
1587+
q.add(node.right);
1588+
}
1589+
}
1590+
1591+
return longestCnt;
1592+
}
1593+
1594+
private int dfsCount(TreeNode root){
1595+
1596+
if (root == null){
1597+
return 0;
1598+
}
1599+
1600+
if (root.left != null && root.val + 1 == root.left.val){
1601+
return this.dfsCount(root.left) + 1;
1602+
}
1603+
1604+
if (root.right != null && root.val + 1 == root.right.val){
1605+
return this.dfsCount(root.right) + 1;
1606+
}
1607+
1608+
// TODO : double check ???
1609+
return Math.max(
1610+
Math.max(this.dfsCount(root.left), this.dfsCount(root.right)),
1611+
this.dfsCount(root)
1612+
);
1613+
1614+
}
1615+
1616+
15571617
}
15581618

15591619

0 commit comments

Comments
 (0)