Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2f32496

Browse files
authored
Create 0129-sum-root-to-leaf-numbers.kt
1 parent 6215af4 commit 2f32496

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
fun sumNumbers(root: TreeNode?): Int {
3+
var res = 0
4+
5+
fun dfs(root: TreeNode?, current: Int) {
6+
root?: return
7+
8+
var new = current * 10
9+
new += root.value
10+
11+
if(root.left == null && root.right == null) {
12+
res += new
13+
return
14+
}
15+
16+
dfs(root.left, new)
17+
dfs(root.right, new)
18+
}
19+
20+
dfs(root, 0)
21+
return res
22+
}
23+
24+
val TreeNode.value get()= this.`val`
25+
}

0 commit comments

Comments
 (0)