Skip to content

Commit 80d0720

Browse files
committed
feat: add solutions to lc problem: No.1430
No.1430.Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary Tree
1 parent cafe9ec commit 80d0720

File tree

40 files changed

+1932
-12
lines changed

40 files changed

+1932
-12
lines changed

solution/1400-1499/1430.Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary Tree/README.md

+104-1
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,125 @@
6060

6161
<!-- 这里可写通用的实现逻辑 -->
6262

63+
DFS。
64+
6365
<!-- tabs:start -->
6466

6567
### **Python3**
6668

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

6971
```python
70-
72+
# Definition for a binary tree node.
73+
# class TreeNode:
74+
# def __init__(self, val=0, left=None, right=None):
75+
# self.val = val
76+
# self.left = left
77+
# self.right = right
78+
class Solution:
79+
def isValidSequence(self, root: TreeNode, arr: List[int]) -> bool:
80+
def dfs(root, u):
81+
if root is None or root.val != arr[u]:
82+
return False
83+
if u == len(arr) - 1:
84+
return root.left is None and root.right is None
85+
return dfs(root.left, u + 1) or dfs(root.right, u + 1)
86+
87+
return dfs(root, 0)
7188
```
7289

7390
### **Java**
7491

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

7794
```java
95+
/**
96+
* Definition for a binary tree node.
97+
* public class TreeNode {
98+
* int val;
99+
* TreeNode left;
100+
* TreeNode right;
101+
* TreeNode() {}
102+
* TreeNode(int val) { this.val = val; }
103+
* TreeNode(int val, TreeNode left, TreeNode right) {
104+
* this.val = val;
105+
* this.left = left;
106+
* this.right = right;
107+
* }
108+
* }
109+
*/
110+
class Solution {
111+
private int[] arr;
112+
113+
public boolean isValidSequence(TreeNode root, int[] arr) {
114+
this.arr = arr;
115+
return dfs(root, 0);
116+
}
117+
118+
private boolean dfs(TreeNode root, int u) {
119+
if (root == null || root.val != arr[u]) {
120+
return false;
121+
}
122+
if (u == arr.length - 1) {
123+
return root.left == null && root.right == null;
124+
}
125+
return dfs(root.left, u + 1) || dfs(root.right, u + 1);
126+
}
127+
}
128+
```
129+
130+
### **C++**
131+
132+
```cpp
133+
/**
134+
* Definition for a binary tree node.
135+
* struct TreeNode {
136+
* int val;
137+
* TreeNode *left;
138+
* TreeNode *right;
139+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
140+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
141+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
142+
* };
143+
*/
144+
class Solution {
145+
public:
146+
bool isValidSequence(TreeNode* root, vector<int>& arr) {
147+
return dfs(root, arr, 0);
148+
}
149+
150+
bool dfs(TreeNode* root, vector<int>& arr, int u) {
151+
if (!root || root->val != arr[u]) return false;
152+
if (u == arr.size() - 1) return !root->left && !root->right;
153+
return dfs(root->left, arr, u + 1) || dfs(root->right, arr, u + 1);
154+
}
155+
};
156+
```
78157

158+
### **Go**
159+
160+
```go
161+
/**
162+
* Definition for a binary tree node.
163+
* type TreeNode struct {
164+
* Val int
165+
* Left *TreeNode
166+
* Right *TreeNode
167+
* }
168+
*/
169+
func isValidSequence(root *TreeNode, arr []int) bool {
170+
var dfs func(root *TreeNode, u int) bool
171+
dfs = func(root *TreeNode, u int) bool {
172+
if root == nil || root.Val != arr[u] {
173+
return false
174+
}
175+
if u == len(arr)-1 {
176+
return root.Left == nil && root.Right == nil
177+
}
178+
return dfs(root.Left, u+1) || dfs(root.Right, u+1)
179+
}
180+
return dfs(root, 0)
181+
}
79182
```
80183

81184
### **...**

solution/1400-1499/1430.Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary Tree/README_EN.md

+104-1
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,121 @@ Other valid sequences are:
5454

5555
## Solutions
5656

57+
DFS.
58+
5759
<!-- tabs:start -->
5860

5961
### **Python3**
6062

6163
```python
62-
64+
# Definition for a binary tree node.
65+
# class TreeNode:
66+
# def __init__(self, val=0, left=None, right=None):
67+
# self.val = val
68+
# self.left = left
69+
# self.right = right
70+
class Solution:
71+
def isValidSequence(self, root: TreeNode, arr: List[int]) -> bool:
72+
def dfs(root, u):
73+
if root is None or root.val != arr[u]:
74+
return False
75+
if u == len(arr) - 1:
76+
return root.left is None and root.right is None
77+
return dfs(root.left, u + 1) or dfs(root.right, u + 1)
78+
79+
return dfs(root, 0)
6380
```
6481

6582
### **Java**
6683

6784
```java
85+
/**
86+
* Definition for a binary tree node.
87+
* public class TreeNode {
88+
* int val;
89+
* TreeNode left;
90+
* TreeNode right;
91+
* TreeNode() {}
92+
* TreeNode(int val) { this.val = val; }
93+
* TreeNode(int val, TreeNode left, TreeNode right) {
94+
* this.val = val;
95+
* this.left = left;
96+
* this.right = right;
97+
* }
98+
* }
99+
*/
100+
class Solution {
101+
private int[] arr;
102+
103+
public boolean isValidSequence(TreeNode root, int[] arr) {
104+
this.arr = arr;
105+
return dfs(root, 0);
106+
}
107+
108+
private boolean dfs(TreeNode root, int u) {
109+
if (root == null || root.val != arr[u]) {
110+
return false;
111+
}
112+
if (u == arr.length - 1) {
113+
return root.left == null && root.right == null;
114+
}
115+
return dfs(root.left, u + 1) || dfs(root.right, u + 1);
116+
}
117+
}
118+
```
119+
120+
### **C++**
121+
122+
```cpp
123+
/**
124+
* Definition for a binary tree node.
125+
* struct TreeNode {
126+
* int val;
127+
* TreeNode *left;
128+
* TreeNode *right;
129+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
130+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
131+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
132+
* };
133+
*/
134+
class Solution {
135+
public:
136+
bool isValidSequence(TreeNode* root, vector<int>& arr) {
137+
return dfs(root, arr, 0);
138+
}
139+
140+
bool dfs(TreeNode* root, vector<int>& arr, int u) {
141+
if (!root || root->val != arr[u]) return false;
142+
if (u == arr.size() - 1) return !root->left && !root->right;
143+
return dfs(root->left, arr, u + 1) || dfs(root->right, arr, u + 1);
144+
}
145+
};
146+
```
68147

148+
### **Go**
149+
150+
```go
151+
/**
152+
* Definition for a binary tree node.
153+
* type TreeNode struct {
154+
* Val int
155+
* Left *TreeNode
156+
* Right *TreeNode
157+
* }
158+
*/
159+
func isValidSequence(root *TreeNode, arr []int) bool {
160+
var dfs func(root *TreeNode, u int) bool
161+
dfs = func(root *TreeNode, u int) bool {
162+
if root == nil || root.Val != arr[u] {
163+
return false
164+
}
165+
if u == len(arr)-1 {
166+
return root.Left == nil && root.Right == nil
167+
}
168+
return dfs(root.Left, u+1) || dfs(root.Right, u+1)
169+
}
170+
return dfs(root, 0)
171+
}
69172
```
70173

71174
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
bool isValidSequence(TreeNode* root, vector<int>& arr) {
15+
return dfs(root, arr, 0);
16+
}
17+
18+
bool dfs(TreeNode* root, vector<int>& arr, int u) {
19+
if (!root || root->val != arr[u]) return false;
20+
if (u == arr.size() - 1) return !root->left && !root->right;
21+
return dfs(root->left, arr, u + 1) || dfs(root->right, arr, u + 1);
22+
}
23+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* type TreeNode struct {
4+
* Val int
5+
* Left *TreeNode
6+
* Right *TreeNode
7+
* }
8+
*/
9+
func isValidSequence(root *TreeNode, arr []int) bool {
10+
var dfs func(root *TreeNode, u int) bool
11+
dfs = func(root *TreeNode, u int) bool {
12+
if root == nil || root.Val != arr[u] {
13+
return false
14+
}
15+
if u == len(arr)-1 {
16+
return root.Left == nil && root.Right == nil
17+
}
18+
return dfs(root.Left, u+1) || dfs(root.Right, u+1)
19+
}
20+
return dfs(root, 0)
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
16+
class Solution {
17+
private int[] arr;
18+
19+
public boolean isValidSequence(TreeNode root, int[] arr) {
20+
this.arr = arr;
21+
return dfs(root, 0);
22+
}
23+
24+
private boolean dfs(TreeNode root, int u) {
25+
if (root == null || root.val != arr[u]) {
26+
return false;
27+
}
28+
if (u == arr.length - 1) {
29+
return root.left == null && root.right == null;
30+
}
31+
return dfs(root.left, u + 1) || dfs(root.right, u + 1);
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def isValidSequence(self, root: TreeNode, arr: List[int]) -> bool:
9+
def dfs(root, u):
10+
if root is None or root.val != arr[u]:
11+
return False
12+
if u == len(arr) - 1:
13+
return root.left is None and root.right is None
14+
return dfs(root.left, u + 1) or dfs(root.right, u + 1)
15+
16+
return dfs(root, 0)

0 commit comments

Comments
 (0)