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

Commitcad71db

Browse files
authored
Create 0337-house-robber-iii.kt
1 parent5ba127b commitcad71db

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

‎kotlin/0337-house-robber-iii.kt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Example:
3+
* var ti = TreeNode(5)
4+
* var v = ti.`val`
5+
* Definition for a binary tree node.
6+
* class TreeNode(var `val`: Int) {
7+
* var left: TreeNode? = null
8+
* var right: TreeNode? = null
9+
* }
10+
*/
11+
classSolution {
12+
funrob(root:TreeNode?):Int {
13+
root?:return0
14+
15+
val res= rob2(root)
16+
return maxOf(
17+
res.first,
18+
res.second
19+
)
20+
}
21+
22+
funrob2(root:TreeNode?):Pair<Int,Int> {
23+
root?:return0 to0
24+
25+
val left= rob2(root.left)
26+
val right= rob2(root.right)
27+
28+
val with= root.`val`+ left.second+ right.second
29+
val without= maxOf(left.first, left.second)+ maxOf(right.first, right.second)
30+
31+
return with to without
32+
}
33+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp