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

Commit 4ee7bf5

Browse files
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
Lines changed: 16 additions & 0 deletions
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)

AltStyle によって変換されたページ (->オリジナル) /