Skip to content

Commit 6536d1a

Browse files
committed
feat: add solutions to lc problem: No.0298
No.0298.Binary Tree Longest Consecutive Sequence
1 parent 935bcb3 commit 6536d1a

File tree

8 files changed

+396
-35
lines changed

8 files changed

+396
-35
lines changed

solution/0200-0299/0298.Binary Tree Longest Consecutive Sequence/README.md

+149-31
Original file line numberDiff line numberDiff line change
@@ -6,65 +6,183 @@
66

77
<!-- 这里写题目描述 -->
88

9-
<p>给你一棵指定的二叉树,请你计算它最长连续序列路径的长度。</p>
9+
<p>给你一棵指定的二叉树的根节点 <code>root</code> ,请你计算其中 <strong>最长连续序列路径</strong> 的长度。</p>
1010

11-
<p>该路径,可以是从某个初始结点到树中任意结点,通过「父 - 子」关系连接而产生的任意路径。</p>
12-
13-
<p>这个最长连续的路径,必须从父结点到子结点,反过来是不可以的。</p>
11+
<p><strong>最长连续序列路径</strong> 是依次递增 1 的路径。该路径,可以是从某个初始节点到树中任意节点,通过「父 - 子」关系连接而产生的任意路径。且必须从父节点到子节点,反过来是不可以的。</p>
12+
&nbsp;
1413

1514
<p><strong>示例 1:</strong></p>
16-
17-
<pre><strong>输入:</strong>
18-
19-
1
20-
\
21-
3
22-
/ \
23-
2 4
24-
\
25-
5
26-
27-
<strong>输出:</strong> <code>3</code>
28-
29-
<strong>解析: </strong>当中,最长连续序列是 <code>3-4-5,所以</code>返回结果为 <code>3</code></pre>
15+
<img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0200-0299/0298.Binary%20Tree%20Longest%20Consecutive%20Sequence/images/consec1-1-tree.jpg" style="width: 322px; height: 421px;" />
16+
<pre>
17+
<strong>输入:</strong>root = [1,null,3,2,4,null,null,null,5]
18+
<strong>输出:</strong>3
19+
<strong>解释:</strong>当中,最长连续序列是 <code>3-4-5 ,所以</code>返回结果为 <code>3 。</code>
20+
</pre>
3021

3122
<p><strong>示例 2:</strong></p>
23+
<img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0200-0299/0298.Binary%20Tree%20Longest%20Consecutive%20Sequence/images/consec1-2-tree.jpg" style="width: 262px; height: 421px;" />
24+
<pre>
25+
<strong>输入:</strong>root = [2,null,3,2,null,1]
26+
<strong>输出:</strong>2
27+
<strong>解释:</strong>当中,最长连续序列是 <code>2-3 。注意,不是</code> <code>3-2-1,所以</code>返回 <code>2 。</code>
28+
</pre>
3229

33-
<pre><strong>输入:
34-
35-
</strong> 2
36-
\
37-
3
38-
/
39-
2
40-
/
41-
1
30+
<p>&nbsp;</p>
4231

43-
<strong>输出: 2
44-
45-
解析: </strong>当中,最长连续序列是 <code>2-3。注意,不是</code> <code>3-2-1,所以</code>返回 <code>2。</code></pre>
32+
<p><strong>提示:</strong></p>
4633

34+
<ul>
35+
<li>树中节点的数目在范围 <code>[1, 3 * 10<sup>4</sup>]</code> 内</li>
36+
<li><code>-3 * 10<sup>4</sup> &lt;= Node.val &lt;= 3 * 10<sup>4</sup></code></li>
37+
</ul>
4738

4839
## 解法
4940

5041
<!-- 这里可写通用的实现逻辑 -->
5142

43+
DFS。
44+
5245
<!-- tabs:start -->
5346

5447
### **Python3**
5548

5649
<!-- 这里可写当前语言的特殊实现逻辑 -->
5750

5851
```python
59-
52+
# Definition for a binary tree node.
53+
# class TreeNode:
54+
# def __init__(self, val=0, left=None, right=None):
55+
# self.val = val
56+
# self.left = left
57+
# self.right = right
58+
class Solution:
59+
def longestConsecutive(self, root: TreeNode) -> int:
60+
def dfs(root, p, t):
61+
nonlocal ans
62+
if root is None:
63+
return
64+
t = t + 1 if p is not None and p.val + 1 == root.val else 1
65+
ans = max(ans, t)
66+
dfs(root.left, root, t)
67+
dfs(root.right, root, t)
68+
69+
ans = 1
70+
dfs(root, None, 1)
71+
return ans
6072
```
6173

6274
### **Java**
6375

6476
<!-- 这里可写当前语言的特殊实现逻辑 -->
6577

6678
```java
79+
/**
80+
* Definition for a binary tree node.
81+
* public class TreeNode {
82+
* int val;
83+
* TreeNode left;
84+
* TreeNode right;
85+
* TreeNode() {}
86+
* TreeNode(int val) { this.val = val; }
87+
* TreeNode(int val, TreeNode left, TreeNode right) {
88+
* this.val = val;
89+
* this.left = left;
90+
* this.right = right;
91+
* }
92+
* }
93+
*/
94+
class Solution {
95+
private int ans;
96+
97+
public int longestConsecutive(TreeNode root) {
98+
ans = 1;
99+
dfs(root, null, 1);
100+
return ans;
101+
}
102+
103+
private void dfs(TreeNode root, TreeNode p, int t) {
104+
if (root == null) {
105+
return;
106+
}
107+
t = p != null && p.val + 1 == root.val ? t + 1 : 1;
108+
ans = Math.max(ans, t);
109+
dfs(root.left, root, t);
110+
dfs(root.right, root, t);
111+
}
112+
}
113+
```
114+
115+
### **C++**
116+
117+
```cpp
118+
/**
119+
* Definition for a binary tree node.
120+
* struct TreeNode {
121+
* int val;
122+
* TreeNode *left;
123+
* TreeNode *right;
124+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
125+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
126+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
127+
* };
128+
*/
129+
class Solution {
130+
public:
131+
int ans;
132+
133+
int longestConsecutive(TreeNode* root) {
134+
ans = 1;
135+
dfs(root, nullptr, 1);
136+
return ans;
137+
}
138+
139+
void dfs(TreeNode* root, TreeNode* p, int t) {
140+
if (!root) return;
141+
t = p != nullptr && p->val + 1 == root-> val ? t + 1 : 1;
142+
ans = max(ans, t);
143+
dfs(root->left, root, t);
144+
dfs(root->right, root, t);
145+
}
146+
};
147+
```
67148
149+
### **Go**
150+
151+
```go
152+
/**
153+
* Definition for a binary tree node.
154+
* type TreeNode struct {
155+
* Val int
156+
* Left *TreeNode
157+
* Right *TreeNode
158+
* }
159+
*/
160+
func longestConsecutive(root *TreeNode) int {
161+
ans := 1
162+
var dfs func(root, p *TreeNode, t int)
163+
dfs = func(root, p *TreeNode, t int) {
164+
if root == nil {
165+
return
166+
}
167+
if p != nil && p.Val+1 == root.Val {
168+
t++
169+
ans = max(ans, t)
170+
} else {
171+
t = 1
172+
}
173+
dfs(root.Left, root, t)
174+
dfs(root.Right, root, t)
175+
}
176+
dfs(root, nil, 1)
177+
return ans
178+
}
179+
180+
func max(a, b int) int {
181+
if a > b {
182+
return a
183+
}
184+
return b
185+
}
68186
```
69187

70188
### **...**

solution/0200-0299/0298.Binary Tree Longest Consecutive Sequence/README_EN.md

+128-2
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,147 @@
3333
<li><code>-3 * 10<sup>4</sup> &lt;= Node.val &lt;= 3 * 10<sup>4</sup></code></li>
3434
</ul>
3535

36-
3736
## Solutions
3837

38+
DFS.
39+
3940
<!-- tabs:start -->
4041

4142
### **Python3**
4243

4344
```python
44-
45+
# Definition for a binary tree node.
46+
# class TreeNode:
47+
# def __init__(self, val=0, left=None, right=None):
48+
# self.val = val
49+
# self.left = left
50+
# self.right = right
51+
class Solution:
52+
def longestConsecutive(self, root: TreeNode) -> int:
53+
def dfs(root, p, t):
54+
nonlocal ans
55+
if root is None:
56+
return
57+
t = t + 1 if p is not None and p.val + 1 == root.val else 1
58+
ans = max(ans, t)
59+
dfs(root.left, root, t)
60+
dfs(root.right, root, t)
61+
62+
ans = 1
63+
dfs(root, None, 1)
64+
return ans
4565
```
4666

4767
### **Java**
4868

4969
```java
70+
/**
71+
* Definition for a binary tree node.
72+
* public class TreeNode {
73+
* int val;
74+
* TreeNode left;
75+
* TreeNode right;
76+
* TreeNode() {}
77+
* TreeNode(int val) { this.val = val; }
78+
* TreeNode(int val, TreeNode left, TreeNode right) {
79+
* this.val = val;
80+
* this.left = left;
81+
* this.right = right;
82+
* }
83+
* }
84+
*/
85+
class Solution {
86+
private int ans;
87+
88+
public int longestConsecutive(TreeNode root) {
89+
ans = 1;
90+
dfs(root, null, 1);
91+
return ans;
92+
}
93+
94+
private void dfs(TreeNode root, TreeNode p, int t) {
95+
if (root == null) {
96+
return;
97+
}
98+
t = p != null && p.val + 1 == root.val ? t + 1 : 1;
99+
ans = Math.max(ans, t);
100+
dfs(root.left, root, t);
101+
dfs(root.right, root, t);
102+
}
103+
}
104+
```
105+
106+
### **C++**
107+
108+
```cpp
109+
/**
110+
* Definition for a binary tree node.
111+
* struct TreeNode {
112+
* int val;
113+
* TreeNode *left;
114+
* TreeNode *right;
115+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
116+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
117+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
118+
* };
119+
*/
120+
class Solution {
121+
public:
122+
int ans;
123+
124+
int longestConsecutive(TreeNode* root) {
125+
ans = 1;
126+
dfs(root, nullptr, 1);
127+
return ans;
128+
}
129+
130+
void dfs(TreeNode* root, TreeNode* p, int t) {
131+
if (!root) return;
132+
t = p != nullptr && p->val + 1 == root-> val ? t + 1 : 1;
133+
ans = max(ans, t);
134+
dfs(root->left, root, t);
135+
dfs(root->right, root, t);
136+
}
137+
};
138+
```
50139
140+
### **Go**
141+
142+
```go
143+
/**
144+
* Definition for a binary tree node.
145+
* type TreeNode struct {
146+
* Val int
147+
* Left *TreeNode
148+
* Right *TreeNode
149+
* }
150+
*/
151+
func longestConsecutive(root *TreeNode) int {
152+
ans := 1
153+
var dfs func(root, p *TreeNode, t int)
154+
dfs = func(root, p *TreeNode, t int) {
155+
if root == nil {
156+
return
157+
}
158+
if p != nil && p.Val+1 == root.Val {
159+
t++
160+
ans = max(ans, t)
161+
} else {
162+
t = 1
163+
}
164+
dfs(root.Left, root, t)
165+
dfs(root.Right, root, t)
166+
}
167+
dfs(root, nil, 1)
168+
return ans
169+
}
170+
171+
func max(a, b int) int {
172+
if a > b {
173+
return a
174+
}
175+
return b
176+
}
51177
```
52178

53179
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
int ans;
15+
16+
int longestConsecutive(TreeNode* root) {
17+
ans = 1;
18+
dfs(root, nullptr, 1);
19+
return ans;
20+
}
21+
22+
void dfs(TreeNode* root, TreeNode* p, int t) {
23+
if (!root) return;
24+
t = p != nullptr && p->val + 1 == root-> val ? t + 1 : 1;
25+
ans = max(ans, t);
26+
dfs(root->left, root, t);
27+
dfs(root->right, root, t);
28+
}
29+
};

0 commit comments

Comments
 (0)