Skip to content

Instantly share code, notes, and snippets.

@cixuuz
Created August 30, 2017 02:05
Show Gist options
  • Save cixuuz/26dfcfbf1f11c6fd8ee908d1c00c65fc to your computer and use it in GitHub Desktop.
Save cixuuz/26dfcfbf1f11c6fd8ee908d1c00c65fc to your computer and use it in GitHub Desktop.
[129. Sum Root to Leaf Numbers] #leetcode
class Solution {
private int res = 0;
public int sumNumbers(TreeNode root) {
if ( root == null ) return 0;
dfs(root, 0);
return res;
}
public void dfs(TreeNode node, int sum) {
if ( node.left == null && node.right == null ) {
res += sum*10 + node.val;
}
if ( node.left != null ) dfs(node.left, sum*10+node.val);
if ( node.right != null ) dfs(node.right, sum*10+node.val);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment