class Solution {
public int dfs(int num,TreeNode root){
if(root!=null){
num = num*10 + root.val;
if(root.left == null && root.right == null){
return num;
}
return dfs(num,root.left)+dfs(num,root.right);
}
return 0;
}
public int sumNumbers(TreeNode root) {
return dfs(0,root);
}
}
深度优先搜索
问题:root可能是null