首页 > 其他分享 >闯关leetcode——112. Path Sum

闯关leetcode——112. Path Sum

时间:2024-10-18 12:46:28浏览次数:3  
标签:return Sum tree leaf targetSum path Path root leetcode

大纲

题目

地址

https://github.com/f304646673/leetcode/tree/main/112-Path-Sum

内容

Given the root of a binary tree and an integer targetSum, return true if the tree has a root-to-leaf path such that adding up all the values along the path equals targetSum.

A leaf is a node with no children.

Example 1:
在这里插入图片描述

Input: root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22
Output: true
Explanation: The root-to-leaf path with the target sum is shown.

Example 2:
在这里插入图片描述

Input: root = [1,2,3], targetSum = 5
Output: false
Explanation: There two root-to-leaf paths in the tree:
(1 --> 2): The sum is 3.
(1 --> 3): The sum is 4.
There is no root-to-leaf path with sum = 5.

Example 3:

Input: root = [], targetSum = 0
Output: false
Explanation: Since the tree is empty, there are no root-to-leaf paths.

Constraints:

  • The number of nodes in the tree is in the range [0, 5000].
  • -1000 <= Node.val <= 1000
  • -1000 <= targetSum <= 1000

解题

这题要检索一棵树从根节点到叶子节点的和,是否存在和给定值一致的情况。
这题的核心是对叶子节点的定义,即没有子节点。这样我们就可以在探索到没有左右子树的节点时,检测剩余未匹配的值是否为0来检测该路径是否符合要求。
只有左右子树中有任何一个路径匹配就直接返回,不用探索出所有可能方案。

class Solution {
public:
    bool hasPathSum(TreeNode* root, int targetSum) {
        if (root == nullptr) return false;

        targetSum -= root->val;
        if (root->left == nullptr && root->right == nullptr) {
            return targetSum == 0;
        }

        if (hasPathSum(root->left, targetSum)) return true;
        if (hasPathSum(root->right, targetSum)) return true;
        return false;
    }
};

在这里插入图片描述

代码地址

https://github.com/f304646673/leetcode/tree/main/112-Path-Sum

标签:return,Sum,tree,leaf,targetSum,path,Path,root,leetcode
From: https://blog.csdn.net/breaksoftware/article/details/142207988

相关文章

  • 闯关leetcode——121. Best Time to Buy and Sell Stock
    大纲题目地址内容解题代码地址题目地址https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/内容Youaregivenanarraypriceswhereprices[i]isthepriceofagivenstockontheithday.Youwanttomaximizeyourprofitby......
  • 讲解LeetCode第150题:逆波兰表达式求值(完整代码)
    LeetCode第150题:逆波兰表达式求值题目介绍方法一:栈完整代码展示核心原理演示代码片段解释片段一:片段二:片段三:片段四:片段五:方法二:数组模拟栈完整代码展示核心原理演示代码片段解释片段一:片段二:片段三:......
  • Leetcode 721. 账户合并
    1.题目基本信息1.1.题目描述给定一个列表accounts,每个元素accounts[i]是一个字符串列表,其中第一个元素accounts[i][0]是名称(name),其余元素是emails表示该账户的邮箱地址。现在,我们想合并这些账户。如果两个账户都有一些共同的邮箱地址,则两个账户必定属于同一个人。请......
  • Leetcode刷题. 贪心算法
    贪心算法:    比较传统的解释:将整个问题拆解为几个小问题,找到小问题的最优解,加起来就是整个问题的全局最优解。对于现在的我理解贪心就是一种感觉,给出证明很难,解题思路一般就是认真读题,发掘题目的条件,然后尝试给出算法。11.盛最多水的容器     一个显而易......
  • LeetCode 209 - 长度最小的子数组(滑动窗口法)
    题目描述给定一个含有n个正整数的数组和一个正整数target,我们要找出该数组中满足其总和大于等于target的长度最小的子数组,即子数组[nums_left,nums_right],并返回其长度。如果不存在符合条件的子数组,返回0。解题思路我们可以使用滑动窗口解决这道问题。初始化左指针......
  • LeetCode:809.情感丰富的文字(双指针 Java)
    目录809.情感丰富的文字题目描述:实现代码与解析:双指针原理思路:809.情感丰富的文字题目描述:        有时候人们会用重复写一些字母来表示额外的感受,比如 "hello"->"heeellooo", "hi"->"hiii"。我们将相邻字母都相同的一串字符定义为相同字母组,例如:"h",......
  • LeetCode LRU 缓存
    题目描述请你设计并实现一个满足LRU(最近最少使用)缓存约束的数据结构。实现LRUCache类:LRUCache(intcapacity)以正整数作为容量capacity初始化LRU缓存intget(intkey)如果关键字key存在于缓存中,则返回关键字的值,否则返回-1。voidput(intkey,intv......
  • leetcode 876. Middle of the Linked List
    leetcode876.MiddleoftheLinkedList不容易出错的写法,慢classSolution{public:ListNode*middleNode(ListNode*head){if(!head||!head->next){returnhead;}ListNode*single=head,*double_=head;int......
  • uniapp精仿微信源码,基于SumerUI和Uniapp前端框架的一款仿微信APP应用,界面漂亮颜值高,视
    uniapp精仿微信源码,基于SumerUI和Uniapp前端框架的一款仿微信APP应用,界面漂亮颜值高,视频商城小工具等,朋友圈视频号即时聊天用于视频,商城,直播,聊天,等等场景,源码分享sumer-weixin介绍uniapp精仿微信,基于SumerUI3.0和Uniapp前端框架的一款仿微信APP应用,界面漂亮颜值高,视频......
  • Leetcode 1514. 概率最大的路径
    1.题目基本信息1.1.题目描述给你一个由n个节点(下标从0开始)组成的无向加权图,该图由一个描述边的列表组成,其中edges[i]=[a,b]表示连接节点a和b的一条无向边,且该边遍历成功的概率为succProb[i]。指定两个节点分别作为起点start和终点end,请你找出从起点到终点成......