原题链接
每次进来如果为空就返回0
如果不为空,计算算上当前节点的值
然后判断当前节点是否为叶子结点,如果是叶子节点就加到答案里
如果不是叶子结点,就返回左子树的和+右子树的和
class Solution {
public:
int dfs(TreeNode* root, int num){
if(!root) return 0;
num = num * 2 + root -> val;
if(!root -> left && ! root -> right) return num;
return dfs(root -> left, num) + dfs(root -> right, num);
}
int sumRootToLeaf(TreeNode* root) {
return dfs(root, 0);
}
};



