力扣-路径总和问题

路径总和 --简单

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

相关推荐
头发够用的程序员7 分钟前
从滑动窗口到矩阵运算:img2col算法基本原理
人工智能·算法·yolo·性能优化·矩阵·边缘计算·jetson
武帝为此31 分钟前
【数据清洗缺失值处理】
python·算法·数学建模
Halo_tjn1 小时前
Java 基于字符串相关知识点
java·开发语言·算法
念越1 小时前
算法每日一题 Day08|双指针法解决三数之和
算法·力扣
黎阳之光2 小时前
黎阳之光透明管理:视频孪生重构智慧仓储新范式
人工智能·算法·安全·重构·数字孪生
6Hzlia2 小时前
【Hot 100 刷题计划】 LeetCode 199. 二叉树的右视图 | C++ DFS 逆序遍历
c++·leetcode·深度优先
CappuccinoRose3 小时前
回溯法 - 软考备战(四十三)
算法·排列组合·路径·n皇后·子集·解数独·岛屿
AC赳赳老秦3 小时前
OpenClaw进阶技巧:批量修改文件内容、替换关键词,解放双手
java·linux·人工智能·python·算法·测试用例·openclaw
Robot_Nav3 小时前
Shape-Aware MPPI(SA MPPI)算法:基于RC-ESDF的任意形状机器人实时轨迹优化
算法·机器人·sa-mppi
踩坑记录4 小时前
leetcode hot100 118. 杨辉三角 easy 动态规划
leetcode·动态规划