题解 | #二叉树中和为某一值的路径(三)#

二叉树中和为某一值的路径(三)

http://www.nowcoder.com/practice/965fef32cae14a17a8e86c76ffe3131f

import java.util.*;

public class Solution {    
    public int sum(TreeNode root, int sum){
        if(root == null)return 0;
        int temp=0;
        if(root.val == sum)temp++;
        return temp+sum(root.left, sum - root.val)+sum(root.right, sum - root.val);
    }
    
    public int FindPath (TreeNode root, int sum) {
        // write code here
        int ans = 0;
        if(root == null)return 0;
        ans += sum(root,sum);
        ans += FindPath(root.left, sum);
        ans += FindPath(root.right, sum);
        return ans;
    }
    
}
全部评论

相关推荐

点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务