Skip to content

Commit aa1f02e

Browse files
committed
Added Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary Tree.java
1 parent 55b76be commit aa1f02e

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
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+
public boolean isValidSequence(TreeNode root, int[] arr) {
18+
return helper(root, arr, 0);
19+
}
20+
21+
private boolean helper(TreeNode root, int[] arr, int idx) {
22+
if (root == null || idx >= arr.length) {
23+
return false;
24+
}
25+
if (root.val != arr[idx]) {
26+
return false;
27+
}
28+
if (root.left == null && root.right == null && idx == (arr.length - 1)) {
29+
return true;
30+
}
31+
return helper(root.left, arr, idx + 1) || helper(root.right, arr, idx + 1);
32+
}
33+
}

0 commit comments

Comments
 (0)