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

Commit2e4dba0

Browse files
authored
Create 1361-validate-binary-tree-nodes.kt
1 parentdbae42b commit2e4dba0

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
classSolution {
2+
funvalidateBinaryTreeNodes(n:Int,leftChild:IntArray,rightChild:IntArray):Boolean {
3+
val hasParent= hashSetOf<Int>().apply {
4+
for (nin (leftChild+ rightChild)) {
5+
if (n!=-1)
6+
this.add(n)
7+
}
8+
}
9+
10+
if (hasParent.size== n)returnfalse
11+
12+
var root=-1
13+
for (iin0 until n) {
14+
if (i!in hasParent) {
15+
root= i
16+
break
17+
}
18+
}
19+
20+
val visit= hashSetOf<Int>()
21+
fundfs(i:Int):Boolean {
22+
if (i==-1)returntrue
23+
if (iin visit)returnfalse
24+
25+
visit.add(i)
26+
27+
return dfs(leftChild[i])&& dfs(rightChild[i])
28+
}
29+
30+
return dfs(root)&& visit.size== n
31+
}
32+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp