Skip to content

Commit

Permalink
Create 0129-sum-root-to-leaf-numbers.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
a93a authored Mar 14, 2023
1 parent 6215af4 commit 2f32496
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions kotlin/0129-sum-root-to-leaf-numbers.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Solution {
fun sumNumbers(root: TreeNode?): Int {
var res = 0

fun dfs(root: TreeNode?, current: Int) {
root?: return

var new = current * 10
new += root.value

if(root.left == null && root.right == null) {
res += new
return
}

dfs(root.left, new)
dfs(root.right, new)
}

dfs(root, 0)
return res
}

val TreeNode.value get()= this.`val`
}

0 comments on commit 2f32496

Please sign in to comment.