【递归】【回溯】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); // 回溯
        }
    }
}
相关推荐
野渡拾光1 小时前
【考研408数据结构-05】 串与KMP算法:模式匹配的艺术
数据结构·考研·算法
bingbingyihao2 小时前
多数据源 Demo
java·springboot
tainshuai3 小时前
用 KNN 算法解锁分类的奥秘:从电影类型到鸢尾花开
算法·分类·数据挖掘
在努力的前端小白7 小时前
Spring Boot 敏感词过滤组件实现:基于DFA算法的高效敏感词检测与替换
java·数据库·spring boot·文本处理·敏感词过滤·dfa算法·组件开发
Coovally AI模型快速验证9 小时前
农田扫描提速37%!基于检测置信度的无人机“智能抽查”路径规划,Coovally一键加速模型落地
深度学习·算法·yolo·计算机视觉·transformer·无人机
pusue_the_sun9 小时前
数据结构:二叉树oj练习
c语言·数据结构·算法·二叉树
一叶飘零_sweeeet9 小时前
从繁琐到优雅:Java Lambda 表达式全解析与实战指南
java·lambda·java8
RaymondZhao349 小时前
【全面推导】策略梯度算法:公式、偏差方差与进化
人工智能·深度学习·算法·机器学习·chatgpt
艾伦~耶格尔10 小时前
【集合框架LinkedList底层添加元素机制】
java·开发语言·学习·面试
zhangfeng113310 小时前
DBSCAN算法详解和参数优化,基于密度的空间聚类算法,特别擅长处理不规则形状的聚类和噪声数据
算法·机器学习·聚类