@@ -54,18 +54,121 @@ Other valid sequences are:
54
54
55
55
## Solutions
56
56
57
+ DFS.
58
+
57
59
<!-- tabs:start -->
58
60
59
61
### ** Python3**
60
62
61
63
``` 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 )
63
80
```
64
81
65
82
### ** Java**
66
83
67
84
``` 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
+ ```
68
147
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
+ }
69
172
```
70
173
71
174
### ** ...**
0 commit comments