From 8c2db95f994015ef2427fdc76e19d86f37c84f0a Mon Sep 17 00:00:00 2001 From: Azureki Date: Mon, 15 Apr 2019 22:15:24 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A0=B9=E5=88=B0=E5=8F=B6=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E5=92=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main.go | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 1022. Sum of Root To Leaf Binary Numbers/main.go diff --git a/1022. Sum of Root To Leaf Binary Numbers/main.go b/1022. Sum of Root To Leaf Binary Numbers/main.go new file mode 100644 index 0000000..a78d037 --- /dev/null +++ b/1022. Sum of Root To Leaf Binary Numbers/main.go @@ -0,0 +1,38 @@ +package main + +import "fmt" + +type TreeNode struct { + Val int + Left *TreeNode + Right *TreeNode +} + +func dFS(root *TreeNode, base int) int { + if root == nil { + return 0 + } + res := 0 + base = (base << 1) + root.Val + if root.Left != nil { + res += dFS(root.Left, base) + } + if root.Right != nil { + res += dFS(root.Right, base) + } + if res != 0 { + + return res + } + return base +} + +func sumRootToLeaf(root *TreeNode) int { + return dFS(root, 0) +} + +func main() { + root := TreeNode{1, nil, nil} + res := sumRootToLeaf(&root) + fmt.Println(res) +}