代码随想录之路经总和

本文介绍了两种解决力扣112题(路径总和)和113题(路径总和II)的方法。第一种是使用递归,从根节点开始,不断减去节点值,当到达叶子节点且targetSum等于0时返回true。第二种方法是利用层序遍历,将父节点值加到子节点上,遍历直到找到目标和。对于路径总和II,采用前序遍历并回溯,记录路径,当找到目标和时添加到结果列表中。
摘要由CSDN通过智能技术生成

路径总和力扣112
路径总和|| 力扣113

本题的思路学习于代码随想录

JAVA版本

一.路径总和:

本题的思路较为简单,只需要用target来减去值即可。
解法一:使用递归的方法

class Solution {
    public boolean hasPathSum(TreeNode root, int targetSum) {
        if (root == null){
            return false;
        }
        targetSum -= root.val;

        if(root.left == null && root.right ==null) {  //根节点,相当于中序
            return targetSum ==0;
        }

        if (root.left != null){
            boolean left = hasPathSum(root.left, targetSum);
            if (left) {      // 已经找到,注意加上判断。
                return true;
            }
        }
        if (root.right != null){
            boolean right = hasPathSum(root.right, targetSum);
            if (right) {      // 已经找到
                return true;
            }
        }
        return false;
    }
}

解法二:使用层序遍历,将父亲节点的值加到子节点当中去,然后使用层序遍历的方式,依次遍历结点,判断结点的值是否与target相同。

class Solution {
    public boolean hasPathSum(TreeNode root, int targetSum) {
        if(root == null) return false;
        Stack <TreeNode> stack1 = new Stack<>(); 
        Stack <Integer> stack2 = new Stack<>();
        stack1.push(root);
        stack2.push(root.val);

        while(!stack1.isEmpty()) {
            int size =stack1.size();
            for (int i =0;i<size;i++){
                TreeNode node = stack1.pop();
                int sum = stack2.pop();

                if (node.left ==null &&node.right ==null && sum == targetSum){
                    return true;
                }

                //使用的是栈,注意栈的入栈顺序
                if (node.left != null) {
                    stack1.push(node.left);
                    stack2.push(sum + node.left.val);
                }

                if (node.right != null) {
                    stack1.push(node.right);
                    stack2.push(sum + node.right.val);
                }
            }
        }   
        return false;   
}
}

二.路径总和||:

本题使用了时间回溯的解法,因为要返回的是具体的路径。
本题与之前做的,求二叉树的所有路径相同。
本题的结束条件需要判断target与每一个值。

class Solution {
    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
        List<List<Integer>> res = new ArrayList<>();
        if (root == null) return res; // 非空判断
        List<Integer> path = new ArrayList<>();//记录路径
        preorderdfs(root, targetSum, res, path);
        return res;
    }

    public void preorderdfs(TreeNode root, int targetsum, List<List<Integer>> res, List<Integer> path) {
         path.add(root.val);
        // 遇到了叶子节点
        if (root.left == null && root.right == null) {
            // 找到了和为 targetsum 的路径
            if (targetsum - root.val == 0) {
                res.add(new ArrayList<>(path));
            }
            return; // 如果和不为 targetsum,返回
        }

         if (root.left != null) {
            preorderdfs(root.left, targetsum - root.val, res, path);
            path.remove(path.size() - 1); // 回溯
        }
        if (root.right != null) {
            preorderdfs(root.right, targetsum - root.val, res, path);
            path.remove(path.size() - 1); // 回溯
        }
    
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值