题解 | #二叉树根节点到叶子节点的所有路径和#
二叉树根节点到叶子节点的所有路径和
http://www.nowcoder.com/practice/185a87cd29eb42049132aed873273e83
package main import . "nc_tools" func sumNumbers( root *TreeNode ) int { // write code here return dfs(root, 0) } func dfs(root *TreeNode, cur int) int { if root == nil { return 0 } cur = cur *10 + root.Val if root.Left == nil && root.Right == nil { return cur } return dfs(root.Left, cur) + dfs(root.Right, cur) }