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 的下降路径的最小和. 下降路径可以 ...
随机推荐
- [BZOJ2251/BJWC2010]外星联络
Description 小 P 在看过电影<超时空接触>(Contact)之后被深深的打动,决心致力于寻找外星人的事业.于是,他每天晚上都爬在屋顶上试图用自己的收音机收听外星人发来的信息. ...
- UVA10305:Ordering Tasks(拓扑排序)
John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...
- Gym - 101810C ACM International Collegiate Programming Contest (2018)
bryce1010模板 http://codeforces.com/gym/101810 #include <bits/stdc++.h> using namespace std; #de ...
- 110 Balanced Binary Tree 平衡二叉树
给定一个二叉树,确定它是高度平衡的.对于这个问题,一棵高度平衡二叉树的定义是:一棵二叉树中每个节点的两个子树的深度相差不会超过 1.案例 1:给出二叉树 [3,9,20,null,null,15,7] ...
- ecshop属性 {$goods.goods_attr|nl2br} 标签的赋值相关
1.nl2br() 函数在字符串中的每个新行 (\n) 之前插入 HTML 换行符 (<br />). 2. 如果要向{$goods.goods_attr|nl2br}赋新值,这个值是保存 ...
- Jquery多选框互相内容交换
<head runat="server"> <title>无标题页</title> <script type="text/jav ...
- jQuery的基本使用及选择器和筛选器
回顾 事件 鼠标clickdblclickcontextmenumouseentermouseleavemousemovemousedownmouseup键盘keydownkeyupkeypress ...
- SQL Server中变量的声明和使用方法
声明局部变量语法:DECLARE @variable_name DataType其中 variable_name为局部变量的名称,DataType为数据类型.给局部变量赋值有两种方法:1.SET @v ...
- win7 x64和win10 x64 windbg 本地调试记录
今天看CSDN和某文档看了win7 64位 和win10 64位 的windbg本地调试内核的办法 win7 x64 Step1:kdbgctrl –db Step2:kdbgctrl –e Step ...
- COGS 696. [IOI1996][USACO 2.3] 最长前缀
★ 输入文件:prefix.in 输出文件:prefix.out 简单对比时间限制:1 s 内存限制:128 MB 描述 USACO 2.3.1 IOI96 在生物学中,一些生物的结构 ...