[Leetcode] Path Sum I,II,III

112. Path Sum I: 点击打开链接

/**
 * Definition for a binary tree node.         //判断是否有这样的路径满足和为sum
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public boolean hasPathSum(TreeNode root, int sum) {
        return helper(root,sum);
    }
    
    public boolean helper(TreeNode root,int sum){
        if(root==null){
            return false;
        }
        
        if(root.left==null && root.right==null){
            if(root.val==sum){
                return true;
            }
        }
        return helper(root.left,sum-root.val) || helper(root.right,sum-root.val);
    }
}

113. Path Sum II: 点击打开链接

/**
 * Definition for a binary tree node.         //写出从根节点开始的和为sum的每一条路径
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public List<List<Integer>> pathSum(TreeNode root, int sum) {
        List<List<Integer>> result=new ArrayList<>();
        List<Integer> path=new ArrayList<>();
        helper(root,sum,result,path);
        return result;
    }
    
    public void helper(TreeNode root,int target,List<List<Integer>> result,List<Integer> path){
        if(root==null){
            return;
        }
        
        if(root.left==null && root.right==null){
            if(root.val==target){
                path.add(root.val);
                result.add(path);
                return;
            }
        }
        
        if(root.left!=null){
            List<Integer> left=new ArrayList<>(path);     //下一层的添加在之前的基础上添加,因此deep copy
            left.add(root.val);                            
            helper(root.left,target-root.val,result,left);
        }
        
        if(root.right!=null){
            List<Integer> right=new ArrayList<>(path);
            right.add(root.val);
            helper(root.right,target-root.val,result,right);
        }
    }
}
437. Path Sum III: 点击打开链接
/**
 * Definition for a binary tree node.           //数出从任意节点开始的和为sum的路径的条数
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public int pathSum(TreeNode root, int sum) {        //根节点开始的满足条件的路径和 + 左子树的情况 + 右子树的情况
        if (root == null) return 0;
        return helper(root, sum) + pathSum(root.left, sum) + pathSum(root.right, sum);
    }
    
    private int helper(TreeNode node, int sum) {        //从任意node开始满足条件的路径和
        if (node == null) return 0;
        int left=helper(node.left, sum - node.val);
        int right=helper(node.right, sum - node.val);
        return (node.val == sum ? 1 : 0) +left+right;
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值