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 the values along the path equals the given sum.
For example:
Given the below binary tree and sum = 22,
return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.
分析
判断给定树中有无 【跟—>叶子】路径节点值之和为 给定sum
递归实现思想:
- 空树,返回false
- 单根节点,判断,若等于sum返回true,否则返回false
- 更新sum -= root—>val 递归判断左右子树,其中一真即真。
AC代码
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool hasPathSum(TreeNode* root, int sum) {
//空树返回false
if (root == NULL)
{
return false;
}
//若是叶子节点,判断返回
else if (!root->left && !root->right)
{
if (root->val == sum)
return true;
else
return false;
}
else
{
//否则,递归判断左右子树
sum -= root->val;
return hasPathSum(root->left, sum) || hasPathSum(root->right, sum);
}
}
};
LeetCode(112) Path Sum的更多相关文章
- LeetCode(113) Path Sum II
题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...
- LeetCode(112):路径总和
Easy! 题目描述: 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和. 说明: 叶子节点是指没有子节点的节点. 示例: 给定如下二叉树,以及 ...
- LeetCode(307) Range Sum Query - Mutable
题目 Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclus ...
- LeetCode(304)Range Sum Query 2D - Immutable
题目 Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper ...
- LeetCode(303)Range Sum Query - Immutable
题目 Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclus ...
- LeetCode(40) Combination Sum II
题目 Given a collection of candidate numbers (C) and a target number (T), find all unique combinations ...
- LeetCode(1)Two Sum
题目: Given an array of integers, find two numbers such that they add up to a specific target number. ...
- LeetCode(39) Combination Sum
题目 Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C w ...
- Leetcode之动态规划(DP)专题-931. 下降路径最小和(Minimum Falling Path Sum)
Leetcode之动态规划(DP)专题-931. 下降路径最小和(Minimum Falling Path Sum) 给定一个方形整数数组 A,我们想要得到通过 A 的下降路径的最小和. 下降路径可以 ...
随机推荐
- [UOJ22]外星人
题解 首先可以发现有效果的\(a_i\)大小一定是递减的,而且一定小于等于当前值 所以我们可以从大到小考虑每个\(a_i\),当确定了一个有效果的\(a_i\)时,\((a_i,x]\)的数都可以随意 ...
- Codeforces 1106F(数论)
要点 998244353的原根g = 3,意味着对于任意\[1 <= x,y<p\]\[x\neq\ y\]\[g^x\%p\neq\ g^y\%p\]因此可以有构造序列\(q(a)与a一 ...
- Hive_Hive的管理_CLI方式
Hive的启动方式- CLI- Web UI- 远程服务启动方式 (1)hive命令行的交互模式,进入hive: hive; hive --service cli; hive -S;(设置Hive静默 ...
- 记录一下filter
filter是什么,如它的字面意思,就是拦截器.它可以在request到达相关资源之前,比如servlet之前先处理requeset,也可以拦截或处理从某个资源比如servlet发出的response ...
- STM32F4之SWO
https://stm32f4-discovery.net/2014/12/library-46-debug-stm32f4-device-swo-feature/
- FTP上传下载 C#辅助类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.N ...
- 在线matlab网站
网址: http://octave-online.net/ 使用:
- siege4压测脚本示例
agent="Siege 1.0"rcconfig="/opt/siege4.0/etc/siegerc"concurrent=$1repet=$2url=&q ...
- RunTests.sh && RunIPhoneSecurityd.sh
https://github.com/gh-unit/gh-unit/blob/master/Scripts/RunTests.sh #!/bin/sh # If we aren't ru ...
- Python实现1-9数组形成的结果为100的所有运算式
问题: 编写一个在1,2,…,9(顺序不能变)数字之间插入+或-或什么都不插入,使得计算结果总是100的程序,并输出所有的可能性.例如:1 + 2 + 34–5 + 67–8 + 9 = 100. f ...