Skip to content

Commit 4ee7bf5

Browse files
committed
feature: add golang solution for leetcode 143
1 parent 10feca7 commit 4ee7bf5

File tree

1 file changed

+16
-0
lines changed
  • solution/0100-0199/0129.Sum Root to Leaf Numbers

1 file changed

+16
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
func sumNumbers(root *TreeNode) int {
2+
if root == nil {
3+
return 0
4+
}
5+
return dfs(root, 0)
6+
}
7+
8+
func dfs(root *TreeNode, sum int) int {
9+
if root == nil {
10+
return 0
11+
}
12+
if root.Left == nil && root.Right == nil {
13+
return 10*sum + root.Val
14+
}
15+
return dfs(root.Left, 10*sum + root.Val) + dfs(root.Right, 10*sum + root.Val)
16+
}

0 commit comments

Comments
 (0)