【递归】【回溯】Leetcode 112. 路径总和 113. 路径总和 II

【递归】【回溯】Leetcode 112. 路径总和 113. 路径总和 II

  • [112. 路径总和](#112. 路径总和)
    • [解法:递归 有递归就有回溯 记得return正确的返回上去](#解法:递归 有递归就有回溯 记得return正确的返回上去)
  • [113. 路径总和 II](#113. 路径总和 II)
    • [解法 递归](#解法 递归)

如果需要搜索整棵二叉树 ,那么递归函数就不要返回值
如果要搜索其中一条符合条件的路径 ,递归函数就需要返回值,因为遇到符合条件的路径了就要及时返回

112. 路径总和

---------------🎈🎈题目链接🎈🎈-------------------

解法:递归 有递归就有回溯 记得return正确的返回上去

count初始等于targetsum,逐次减,如果到了叶子结点正好count为0,那么就返回true

终止条件:if(root.left = null && root.right = null && count=0){ return true; }

时间复杂度O(N)

空间复杂度O(N)

java 复制代码
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public boolean hasPathSum(TreeNode root, int targetSum) {
        // 终止条件
        if(root == null) return false;
        int count = targetSum-root.val;
        return help(root,count);
    }
    
    public boolean help(TreeNode root, int count){
        if(root.left==null && root.right==null && count==0){
            return true;
        }
        if(root.left==null && root.right==null && count!=0){
            return false;
        }

        // 左
        if(root.left != null){
            if(help(root.left, count-root.left.val)){
                return true;
            }
        }
       
        // 右
        if(root.right != null){
            if(help(root.right, count-root.right.val)){
                return true;
            }
        }
        return false;

    }
    
}

113. 路径总和 II

---------------🎈🎈题目链接🎈🎈-------------------

解法 递归

时间复杂度O(N)

空间复杂度O(N)

java 复制代码
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    List<List<Integer>> finalresult = new ArrayList<>();
    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
        List<Integer> result = new ArrayList<>();
        if(root == null) return finalresult;
        result.add(root.val);
        helper(root,targetSum-root.val,result);
        return finalresult;

    }
    public void helper(TreeNode root, int count, List<Integer> result){

        if(root.left == null && root.right==null && count==0){
            finalresult.add(new ArrayList<>(result)); 
            // 这里千万不能finalresult.add(result) 这就成了添加result的引用,每次都会变
        }

        // 左
        if(root.left != null){
            result.add(root.left.val);
            helper(root.left, count-root.left.val,result);
            result.remove(result.size()-1); // 回溯
        }
        // 右
        if(root.right != null){
            result.add(root.right.val);
            helper(root.right,count-root.right.val,result);
            result.remove(result.size()-1); // 回溯
        }
    }
}
相关推荐
DoraBigHead38 分钟前
小哆啦解题记——异位词界的社交网络
算法
麦兜*42 分钟前
Spring Boot 企业级动态权限全栈深度解决方案,设计思路,代码分析
java·spring boot·后端·spring·spring cloud·性能优化·springcloud
木头左2 小时前
逻辑回归的Python实现与优化
python·算法·逻辑回归
ruan1145142 小时前
MySQL4种隔离级别
java·开发语言·mysql
Hellyc6 小时前
基于模板设计模式开发优惠券推送功能以及对过期优惠卷进行定时清理
java·数据库·设计模式·rocketmq
lifallen6 小时前
Paimon LSM Tree Compaction 策略
java·大数据·数据结构·数据库·算法·lsm-tree
hdsoft_huge6 小时前
SpringBoot 与 JPA 整合全解析:架构优势、应用场景、集成指南与最佳实践
java·spring boot·架构
百锦再7 小时前
详细解析 .NET 依赖注入的三种生命周期模式
java·开发语言·.net·di·注入·模式·依赖
程序员的世界你不懂7 小时前
基于Java+Maven+Testng+Selenium+Log4j+Allure+Jenkins搭建一个WebUI自动化框架(2)对框架加入业务逻辑层
java·selenium·maven
web_Hsir8 小时前
vue3.2 前端动态分页算法
前端·算法