leetcode112路径总和刷题打卡

简介: leetcode112路径总和刷题打卡
112. 路径总和

题目描述

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

题解思路-----正在艰难的学习回溯。。。

  • 递归法
  • 包含了回溯,回溯我现在的理解就是可以访问未知输入的每一种情况
  • 本题就是利用回溯来枚举每一种情况,查看是否符合题目要求
  • 思路是设置一个count来等于targetSum,然后每搜索到一个节点就将count减去该节点的值,如果到了叶子节点,count为0,则返回false

递归三件套

  • 确定参数与返回值
  • 参数为根节点和count,返回一个bool值
  • 确定终止条件
  • 当该节点是叶子节点且count降为了0返回true
  • 当该节点是叶子节点且count不为0返回false
  • 确定本层逻辑
  • 递归函数是有返回值的,如果递归函数返回true,说明找到了合适的路径,应该立刻返回。
  • 回溯的部分也是在这里
if(root->left){
    count -= root->left->val;  //回溯
    if(backTravaling(root->left,count)) return true;
    count += root->left->val;  //回溯
}
if(root->right){
  count -= root->right->val;  //回溯
    if(backTravaling(root->right,count)) return true;
    count += root->right->val;  //回溯
}
  • 简化版
if(root->left) 
    if(backTracking(root->left, count - root->left->val)) return true;
if(root->right)
    if(backTracking(root->right, count - root->right->val)) return true;

完整代码:

class Solution {
public:
    bool backTravaling(TreeNode * root, int count){
        if(!root->left&&!root->right&& count == 0) return true;  //中
        if(!root->left&&!root->right) return false;
        if(root->left){  //左
            count -= root->left->val;
            if(backTravaling(root->left,count)) return true;
            count += root->left->val;
        }
        if(root->right){  //右
            count -= root->right->val;
            if(backTravaling(root->right,count)) return true;
            count += root->right->val;
        }
        return false;
    }
    bool hasPathSum(TreeNode* root, int targetSum) {
        if(!root) return false;
        int sum = targetSum;
        return backTravaling(root,targetSum - root->val);
    }
};


相关文章
|
12天前
【LeetCode 35】112.路径总和
【LeetCode 35】112.路径总和
19 0
|
28天前
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
|
12天前
【LeetCode 36】113.路径总和II
【LeetCode 36】113.路径总和II
25 0
|
2月前
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
50 6
|
2月前
|
搜索推荐 索引 Python
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
99 2
|
28天前
|
数据采集 负载均衡 安全
LeetCode刷题 多线程编程九则 | 1188. 设计有限阻塞队列 1242. 多线程网页爬虫 1279. 红绿灯路口
本文提供了多个多线程编程问题的解决方案,包括设计有限阻塞队列、多线程网页爬虫、红绿灯路口等,每个问题都给出了至少一种实现方法,涵盖了互斥锁、条件变量、信号量等线程同步机制的使用。
LeetCode刷题 多线程编程九则 | 1188. 设计有限阻塞队列 1242. 多线程网页爬虫 1279. 红绿灯路口
|
12天前
【LeetCode 34】257.二叉树的所有路径
【LeetCode 34】257.二叉树的所有路径
8 0
|
2月前
|
存储 算法 Linux
LeetCode第71题简化路径
文章讲述了LeetCode第71题"简化路径"的解题方法,利用栈的数据结构特性来处理路径中的"."和"..",实现路径的简化。
LeetCode第71题简化路径
|
2月前
|
算法
LeetCode第64题最小路径和
LeetCode第64题"最小路径和"的解题方法,运用动态规划思想,通过构建一个dp数组来记录到达每个点的最小路径和,从而高效求解。
LeetCode第64题最小路径和
|
2月前
|
索引 Python
【Leetcode刷题Python】从列表list中创建一颗二叉树
本文介绍了如何使用Python递归函数从列表中创建二叉树,其中每个节点的左右子节点索引分别是当前节点索引的2倍加1和2倍加2。
47 7