力扣-路径总和问题

路径总和 --简单

112. 路径总和

给你二叉树的根节点 root 和一个表示目标和的整数 targetSum 。判断该树中是否存在 根节点到叶子节点 的路径,这条路径上所有节点值相加等于目标和 targetSum 。如果存在,返回 true ;否则,返回 false

叶子节点 是指没有子节点的节点。

直接果断dfs做题就行了。

这里是C的代码

复制代码
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
bool hasPathSum(struct TreeNode* root, int targetSum){
    if(root==NULL)
    return false;
    else
        if(targetSum==root->val&&root->right==NULL&&root->left==NULL)
        return true;
        else
        return (hasPathSum((root->right),targetSum-root->val))+(hasPathSum((root->left),targetSum-root->val));
    
}

这里是Java的代码

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;
        else if(root.left == null && root.right == null && targetSum == root.val)
            return true;
        else 
            return (hasPathSum((root.right),targetSum-root.val)) ||(hasPathSum((root.left),targetSum-root.val));
    }
}

路径总和2 --中等

113. 路径总和 II

给你二叉树的根节点 root 和一个整数目标和 targetSum ,找出所有 从根节点到叶子节点 路径总和等于给定目标和的路径。

叶子节点 是指没有子节点的节点

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 {
    LinkedList<List<Integer>> res = new LinkedList<>();//最后返回的结果集合
    LinkedList<Integer> path = new LinkedList<>();    //路径集合

    void dfs(TreeNode root,int targetSum){
        if(root == null) {
            return;
        }
            
        
        path.add(root.val);
        dfs(root.left,targetSum-root.val);
        dfs(root.right,targetSum-root.val);     
        if(targetSum == root.val && root.left == null && root.right == null)
            res.add(new LinkedList(path));    //感觉最重要的就是这里了,new LinkedList(path)直接把path的值赋给新的对象了。

        path.removeLast();    
    }

    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
        if(root == null)
            return res;
        dfs(root,targetSum);
        return res;
    }
}

路径总和3

437. 路径总和 III

给定一个二叉树的根节点 root ,和一个整数 targetSum ,求该二叉树里节点值之和等于 targetSum路径 的数目。

路径 不需要从根节点开始,也不需要在叶子节点结束,但是路径方向必须是向下的(只能从父节点到子节点)。

这个题目就是求每个节点的到另一个节点路径总和会等于targetsum吗?

所以直接得出:直接对每个结点进行dfs就可以解决了。

这个题目还有一种做法,就是用bfs+dfs来做,bfs遍历树,然后dfs求每个结点的路径总和。

这个题目有个注意点,就是测试数据中,有个很大的结点值,必须要用long来接收,int会越界出错,属实让我触不及防了。

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 int pathSum(TreeNode root, int targetSum) { if(root==null){ return 0; }

    int res = 0;

    long ts = (long) targetSum;

    res += PointCount(root, ts);

    res += pathSum(root.left, (int)ts);
    res += pathSum(root.right, (int)ts);

    return res;

}

public int PointCount(TreeNode root, long targetSum){

    int res = 0;

    if(root == null){
        return 0;
    }

    long val = (long) root.val;

    if(val == targetSum){
        res++;
    }
    

    res += PointCount(root.left, targetSum-val);

    res += PointCount(root.right, targetSum-val);

    return res;
}
}

总结

其实这种路径问题就是围绕dfs和bfs来做题的。掌握好dfs和bfs就可以基本拿捏这种类型的题目了。

相关推荐
Yingye Zhu(HPXXZYY)2 小时前
ICPC 2023 Nanjing R L 题 Elevator
算法
阿维的博客日记4 小时前
LeetCode 139. 单词拆分 - 动态规划解法详解
leetcode·动态规划·代理模式
程序员Xu5 小时前
【LeetCode热题100道笔记】二叉树的右视图
笔记·算法·leetcode
笑脸惹桃花5 小时前
50系显卡训练深度学习YOLO等算法报错的解决方法
深度学习·算法·yolo·torch·cuda
阿维的博客日记6 小时前
LeetCode 48 - 旋转图像算法详解(全网最优雅的Java算法
算法·leetcode
GEO_YScsn6 小时前
Rust 的生命周期与借用检查:安全性深度保障的基石
网络·算法
程序员Xu6 小时前
【LeetCode热题100道笔记】二叉搜索树中第 K 小的元素
笔记·算法·leetcode
THMAIL7 小时前
机器学习从入门到精通 - 数据预处理实战秘籍:清洗、转换与特征工程入门
人工智能·python·算法·机器学习·数据挖掘·逻辑回归
Kevinhbr7 小时前
CSP-J/S IS COMING
数据结构·c++·算法
蕓晨8 小时前
set的插入和pair的用法
c++·算法