112.Path Sum 路径总和

60 篇文章 0 订阅
45 篇文章 0 订阅

Path Sum I 路径总和 I

代码部分一(0ms):

class Solution {
    boolean res = false;
    public boolean hasPathSum(TreeNode root, int sum) {
        if(root == null)
            return false;
        int count = 0;
        isHasPathSum(root, sum, count);
        return res;
    }
    
    public void isHasPathSum(TreeNode root, int sum, int count){
        count += root.val;
        if(root.left == null && root.right == null){
            if(count == sum){
                res = true;
                return;
            }
            count = 0;
        }
        if(root.left != null){
            isHasPathSum(root.left, sum, count);
        }
        if(root.right != null){
            isHasPathSum(root.right, sum, count);
        }
    }
}
  1. 若树为空,返回false

  2. 调用isHasPathSum()

  3. 将当前节点的值 root.val 增到 count ,判断当前节点是否叶子节点,此路径总值 count 是否等于 sum,是则return true

  4. 否则 count 置 0,进行下一路径遍历。

  5. 返回 res

 

代码部分二(0ms):

class Solution {
    public boolean hasPathSum(TreeNode root, int sum) {
		
		if(root == null) return false;
		
		if (root.left == null && root.right == null) return sum == root.val;
		
		if (root.left == null) return hasPathSum(root.right, sum-root.val);

		if (root.right == null) return hasPathSum(root.left, sum-root.val);
				
		return hasPathSum(root.left, sum-root.val) || hasPathSum(root.right, sum-root.val);
        
    }
}
  1. 若树为空 ,返回个false

  2. 若当前节点为叶子节点,将其赋值为 sum,并返回该值

  3. 对左右子节点进行遍历

  4. 若左右路径遍历中有存在路径总值为 sum ,返回true

  5. 将目标值sum,逐个数值递减,当遍历到子叶节点时,sum 应 == root.val

 

 

Path Sum III 路径总和 III

代码部分一(27ms):

class Solution {
    public int pathSum(TreeNode root, int sum) {
        if(root == null)
            return 0;
        return countSum(root, sum, 0) + pathSum(root.left, sum) + pathSum(root.right, sum);
    }
    public int countSum(TreeNode node, int sum, int pre){
        if(node == null)
            return 0;
        pre += node.val;
        int res = 0;
        if(pre == sum)
            res = 1;
        return res + countSum(node.left, sum, pre)+countSum(node.right, sum, pre);
    }
}
  1. 若树为空,返回 0

  2. 调用countSum(),对左右子树进行递归

  3. 向pre中增加一条路径上的每个节点的值

  4. 若sum 等于 pre 时,返回1

  5. 递归countSum()

  6. 返回值

代码部分二(16ms):

class Solution {
    int counter = 0;
    public int pathSum(TreeNode root, int sum) {  
        HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
        map.put(0,1);
        dfs(root, 0, sum, map);
        return counter;
    }
    private void dfs(TreeNode root, int sum, int target, HashMap<Integer, Integer> map){
        
        if(root == null){
            return;
        }
        sum += root.val;
        if(map.containsKey(sum - target)){
            counter+=map.get(sum - target);
        }
        
        if(map.containsKey(sum)){
            map.put(sum, map.get(sum)+1);
        }else{
            map.put(sum,1);
        }
        dfs(root.left, sum, target, map);
        dfs(root.right, sum, target, map);
       
        map.put(sum, map.get(sum)-1);
        
    }
}
  1. 创建 HashMap 

  2. 进行dfs()深度遍历

  3. 若节点为空,返回

  4. 判断 map 中是否记录了 sum-target ,是则counter+1()

  5. 若map 中不存在 sum ,存入新纪录(sum,1),若存在,则记录加1

  6. 左右子树开始遍历

  7. 递归返回过程,将记录-1

  8. 在路径遍历过程,先不断将记录存入map, 并不断判断知道找到符合值。相同的值之所以要记录,因为当遍历至某节点,可能左右节点都可以令其满足

 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值