Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.1430
Browse files Browse the repository at this point in the history
No.1430.Check If a String Is a Valid Sequence from Root to Leaves Path
in a Binary Tree
  • Loading branch information
yanglbme committed Jan 9, 2022
1 parent cafe9ec commit 80d0720
Show file tree
Hide file tree
Showing 40 changed files with 1,932 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,125 @@

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

DFS。

<!-- tabs:start -->

### **Python3**

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

```python

# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def isValidSequence(self, root: TreeNode, arr: List[int]) -> bool:
def dfs(root, u):
if root is None or root.val != arr[u]:
return False
if u == len(arr) - 1:
return root.left is None and root.right is None
return dfs(root.left, u + 1) or dfs(root.right, u + 1)

return dfs(root, 0)
```

### **Java**

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

```java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
private int[] arr;

public boolean isValidSequence(TreeNode root, int[] arr) {
this.arr = arr;
return dfs(root, 0);
}

private boolean dfs(TreeNode root, int u) {
if (root == null || root.val != arr[u]) {
return false;
}
if (u == arr.length - 1) {
return root.left == null && root.right == null;
}
return dfs(root.left, u + 1) || dfs(root.right, u + 1);
}
}
```

### **C++**

```cpp
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
bool isValidSequence(TreeNode* root, vector<int>& arr) {
return dfs(root, arr, 0);
}

bool dfs(TreeNode* root, vector<int>& arr, int u) {
if (!root || root->val != arr[u]) return false;
if (u == arr.size() - 1) return !root->left && !root->right;
return dfs(root->left, arr, u + 1) || dfs(root->right, arr, u + 1);
}
};
```

### **Go**

```go
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func isValidSequence(root *TreeNode, arr []int) bool {
var dfs func(root *TreeNode, u int) bool
dfs = func(root *TreeNode, u int) bool {
if root == nil || root.Val != arr[u] {
return false
}
if u == len(arr)-1 {
return root.Left == nil && root.Right == nil
}
return dfs(root.Left, u+1) || dfs(root.Right, u+1)
}
return dfs(root, 0)
}
```

### **...**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,121 @@ Other valid sequences are:

## Solutions

DFS.

<!-- tabs:start -->

### **Python3**

```python

# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def isValidSequence(self, root: TreeNode, arr: List[int]) -> bool:
def dfs(root, u):
if root is None or root.val != arr[u]:
return False
if u == len(arr) - 1:
return root.left is None and root.right is None
return dfs(root.left, u + 1) or dfs(root.right, u + 1)

return dfs(root, 0)
```

### **Java**

```java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
private int[] arr;

public boolean isValidSequence(TreeNode root, int[] arr) {
this.arr = arr;
return dfs(root, 0);
}

private boolean dfs(TreeNode root, int u) {
if (root == null || root.val != arr[u]) {
return false;
}
if (u == arr.length - 1) {
return root.left == null && root.right == null;
}
return dfs(root.left, u + 1) || dfs(root.right, u + 1);
}
}
```

### **C++**

```cpp
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
bool isValidSequence(TreeNode* root, vector<int>& arr) {
return dfs(root, arr, 0);
}

bool dfs(TreeNode* root, vector<int>& arr, int u) {
if (!root || root->val != arr[u]) return false;
if (u == arr.size() - 1) return !root->left && !root->right;
return dfs(root->left, arr, u + 1) || dfs(root->right, arr, u + 1);
}
};
```

### **Go**

```go
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func isValidSequence(root *TreeNode, arr []int) bool {
var dfs func(root *TreeNode, u int) bool
dfs = func(root *TreeNode, u int) bool {
if root == nil || root.Val != arr[u] {
return false
}
if u == len(arr)-1 {
return root.Left == nil && root.Right == nil
}
return dfs(root.Left, u+1) || dfs(root.Right, u+1)
}
return dfs(root, 0)
}
```

### **...**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
bool isValidSequence(TreeNode* root, vector<int>& arr) {
return dfs(root, arr, 0);
}

bool dfs(TreeNode* root, vector<int>& arr, int u) {
if (!root || root->val != arr[u]) return false;
if (u == arr.size() - 1) return !root->left && !root->right;
return dfs(root->left, arr, u + 1) || dfs(root->right, arr, u + 1);
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func isValidSequence(root *TreeNode, arr []int) bool {
var dfs func(root *TreeNode, u int) bool
dfs = func(root *TreeNode, u int) bool {
if root == nil || root.Val != arr[u] {
return false
}
if u == len(arr)-1 {
return root.Left == nil && root.Right == nil
}
return dfs(root.Left, u+1) || dfs(root.Right, u+1)
}
return dfs(root, 0)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
private int[] arr;

public boolean isValidSequence(TreeNode root, int[] arr) {
this.arr = arr;
return dfs(root, 0);
}

private boolean dfs(TreeNode root, int u) {
if (root == null || root.val != arr[u]) {
return false;
}
if (u == arr.length - 1) {
return root.left == null && root.right == null;
}
return dfs(root.left, u + 1) || dfs(root.right, u + 1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def isValidSequence(self, root: TreeNode, arr: List[int]) -> bool:
def dfs(root, u):
if root is None or root.val != arr[u]:
return False
if u == len(arr) - 1:
return root.left is None and root.right is None
return dfs(root.left, u + 1) or dfs(root.right, u + 1)

return dfs(root, 0)
Loading

0 comments on commit 80d0720

Please sign in to comment.