Created
August 30, 2017 02:05
-
-
Save cixuuz/26dfcfbf1f11c6fd8ee908d1c00c65fc to your computer and use it in GitHub Desktop.
[129. Sum Root to Leaf Numbers] #leetcode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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