力扣-路径总和问题

路径总和 --简单

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就可以基本拿捏这种类型的题目了。

相关推荐
好吃的肘子1 小时前
Elasticsearch架构原理
开发语言·算法·elasticsearch·架构·jenkins
胡耀超1 小时前
霍夫圆变换全面解析(OpenCV)
人工智能·python·opencv·算法·计算机视觉·数据挖掘·数据安全
软行1 小时前
LeetCode 每日一题 3341. 到达最后一个房间的最少时间 I + II
数据结构·c++·算法·leetcode·职场和发展
nlog3n1 小时前
Go语言交替打印问题及多种实现方法
开发语言·算法·golang
How_doyou_do2 小时前
备战菊厂笔试4
python·算法·leetcode
朱剑君2 小时前
第九天——贪心算法——非递减数组
算法·贪心算法
Wnq100722 小时前
工业场景轮式巡检机器人纯视觉识别导航的优势剖析与前景展望
人工智能·算法·计算机视觉·激光雷达·视觉导航·人形机器人·巡检机器人
天上路人4 小时前
AI神经网络降噪算法在语音通话产品中的应用优势与前景分析
深度学习·神经网络·算法·硬件架构·音视频·实时音视频
好吃的肘子4 小时前
MongoDB 应用实战
大数据·开发语言·数据库·算法·mongodb·全文检索
汉克老师4 小时前
GESP2025年3月认证C++二级( 第三部分编程题(1)等差矩阵)
c++·算法·矩阵·gesp二级·gesp2级