package code;
/*
* 129. Sum Root to Leaf Numbers
*
* Medium
* Tree, Depth-first Search
* dfs
* Tipslc112, lc113, lc437, lc129, lc124, lc337
*/
public class lc129 {
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
public int sumNumbers(TreeNode root) {
if(root==null) return 0;
return helper(root, 0);
}
public int helper(TreeNode root, int sum){
if(root==null) return 0; //0
if(root.left==null&&root.right==null) return sum*10+root.val; //
return helper(root.left, sum*10+root.val) + helper(root.right, sum*10+root.val);
}
}