leetcode 38:path-sum
题目描述
例如:
给出如下的二叉树,sum=22,
5
/ \
4 8
/ / \
11 13 4
/ \ \
7 2 1
返回true,因为存在一条路径5->4->11->2的节点值之和为22
Given a binary tree and a sum, determine if the tree has a root-to-leaf
path such that adding up all the values along the path equals the given
sum.
For example:
Given the below binary tree andsum = 22,
5
/ \
4 8
/ / \
11 13 4
/ \ \
7 2 1
return true, as there exist a root-to-leaf path5->4->11->2which sum is 22.
输出
true
/**
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
class Solution {
public:
/**
*
* @param root TreeNode类
* @param sum int整型
* @return bool布尔型
*/
bool hasPathSum(TreeNode* root, int sum) {
// write code here
if (root==NULL){
return false;
}
if(root->left ==NULL && root->right==NULL && sum-root->val==0)
return true;
return hasPathSum(root->left, sum-root->val)||hasPathSum(root->right, sum-root->val);
}
};
leetcode 38:path-sum的更多相关文章
- [LeetCode] 437. Path Sum III_ Easy tag: DFS
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] 112. Path Sum 路径和
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- [LeetCode] 113. Path Sum II 路径和 II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- [LeetCode] 437. Path Sum III 路径和 III
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] 666. Path Sum IV 二叉树的路径和 IV
If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...
- LeetCode 437. Path Sum III (路径之和之三)
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] 113. Path Sum II ☆☆☆(二叉树所有路径和等于给定的数)
LeetCode 二叉树路径问题 Path SUM(①②③)总结 Path Sum II leetcode java 描述 Given a binary tree and a sum, find al ...
- [LeetCode] 112. Path Sum ☆(二叉树是否有一条路径的sum等于给定的数)
Path Sum leetcode java 描述 Given a binary tree and a sum, determine if the tree has a root-to-leaf pa ...
- 动态规划小结 - 二维动态规划 - 时间复杂度 O(n*n)的棋盘型,题 [LeetCode] Minimum Path Sum,Unique Paths II,Edit Distance
引言 二维动态规划中最常见的是棋盘型二维动态规划. 即 func(i, j) 往往只和 func(i-1, j-1), func(i-1, j) 以及 func(i, j-1) 有关 这种情况下,时间 ...
- [Leetcode Week14]Path Sum II
Path Sum II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/path-sum-ii/description/ Description Giv ...
随机推荐
- 《Android逆向反编译代码注入》 - 逆向安全入门必看视频教程
适合人群: Android开发人员.逆向反编译开发人员.以及对Android逆向安全感兴趣的朋友. 视频地址: 51CTO学院:https://edu.51cto.com/course/24485 ...
- GDB将所有线程堆栈输出到文件
在调试多线程程序时,经常需要查看线程堆栈信息,如果线程数目过多,每次查看一个线程堆栈,繁琐耗时.下面介绍一种一次性将所有线程堆栈输出到文件的方法. 首先,将gdb attach到调试线程 gdb -p ...
- mysql DISTINCT选取多个字段,获取distinct后的行信息
背景: a表保存关联关系,通过ACode 获取该关系中的所有 BCode, 并获取所有Bcode-max(Bvrsn)的信息 Bnm 表a 表b 循序渐进: ...
- 发布TrajStat 1.4.4
发布TrajStat 1.4.4 (MeteoInfo Java版插件),在PSCF和CWT源区分析中增加了依据格点中的气团轨迹条数为权重进行结果处理的功能.之前的仅有按照轨迹节点数目进行权重处理的功 ...
- spring-security-结合JWT的简单demo
spring-security-demo 前言:本来是想尽量简单简单点的写一个demo的,但是spring-security实在是内容有点多,写着写着看起来就没那么简单了,想入门spring-secu ...
- IDEA 半天卡住buid(编译)不动——解决办法(适用于maven及gradle)及定位思路
[号外号外!] 最终解决办法并不复杂,关键在于"遇见问题,怎么样层层分析,多条路径试错,最终解决问题的思路或者能力"--资深码农的核心竞争力之一 背景 今天结束完最近2个月的一个项 ...
- springboot入门系列(一):简单搭建springboot项目
Spring Boot 简单介绍 Spring Boot 本身并不提供Spring框架的核心特性以及扩展功能,只是用于快速.敏捷地开发新一代基于Spring框架的应用程序.也就是说,它并不是用来替代S ...
- OpenTelemetry架构介绍
OpenTelemetry: 经得起考验的工具 摘自:https://blog.newrelic.com/product-news/what-is-opentelemetry/ 目录 OpenTele ...
- 【16】进大厂必须掌握的面试题-100个python面试
我们整理了Python面试的主要问题清单,分为7个部分: 基本面试问题 OOPS面试问题 基本的Python程序 Python库面试问题 数据分析面试题 选择题(MCQ) 基本的Python面试问题 ...
- 【实战】记一次老项目的swagger整合
1.背景 这两天接到一个整合swagger的任务,本以为很简单,预计两小时内完成,没想到其中有太多的坑,整了两天才完成. 首先项目是一个比较老的项目,之前用的servlet,目前在重构为springm ...