/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int sumNumbers(TreeNode root) {
return sum(root, 0);
}
public int sum(TreeNode root, int temp) {
if (root==null) return 0;
temp = 10*temp + root.val;
if (root.left==null && root.right==null) return temp;
return (sum(root.left, temp) + sum(root.right, temp));
}
}
没有评论:
发表评论