Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitaa1f02e

Browse files
committed
Added Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary Tree.java
1 parent55b76be commitaa1f02e

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+
classSolution {
17+
publicbooleanisValidSequence(TreeNoderoot,int[]arr) {
18+
returnhelper(root,arr,0);
19+
}
20+
21+
privatebooleanhelper(TreeNoderoot,int[]arr,intidx) {
22+
if (root ==null ||idx >=arr.length) {
23+
returnfalse;
24+
}
25+
if (root.val !=arr[idx]) {
26+
returnfalse;
27+
}
28+
if (root.left ==null &&root.right ==null &&idx == (arr.length -1)) {
29+
returntrue;
30+
}
31+
returnhelper(root.left,arr,idx +1) ||helper(root.right,arr,idx +1);
32+
}
33+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp