Thanks to visit codestin.com Credit goes to github.com
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 6215af4 commit 2f32496Copy full SHA for 2f32496
kotlin/0129-sum-root-to-leaf-numbers.kt
@@ -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