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 的下降路径的最小和. 下降路径可以 ...
随机推荐
- kojs select绑值(工作)
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <tit ...
- 关于java中的不可变类(转)
如何在Java中写出Immutable的类? 要写出这样的类,需要遵循以下几个原则: 1)immutable对象的状态在创建之后就不能发生改变,任何对它的改变都应该产生一个新的对象. 2)Immuta ...
- Spring+JCaptcha验证码使用示例
1,导入jcaptcha.jar包,这里用的是1.0版本 2,编写captcha-context.xml配置文件(非必须,可在spring配置文件中直接添加): <?xml version=&q ...
- HTML超链接的使用
基本语法 <a href="" target="打开方式" name="页面锚点名称">链接文字或图片</a> 属性 ...
- 用ssh-key-gen 在本地主机上创建公钥和密钥
用ssh-key-gen 在本地主机上创建公钥和密钥 ligh@local-host$ ssh-keygen -t rsa
- hdu1754I Hate It(splay)
链接 线段树的水题,拿来学习一下splay. 本题涉及到求最大值以及单点更新,折腾了许久,差不多把splay搞明白了. 按位置建树,按位置是一颗排序二叉树,对于区间的操作非常方便,每次操作都将需要的结 ...
- 数据库 join
数据库 join 最近有个项目用到了数据库,写 sql 时有联表查询,不知道怎么写,怎过滤,查了一些资料,补充了一些知识点. 以下是链接: 维基百科 JOIN关联表中ON,WHERE后面跟条件的区别 ...
- SpringBoot 2.x (12):整合Elasticsearch
Elasticsearch:一个优秀的搜索引擎框架 搜索方面最基本的是SQL的like语句 进一步的有Lucene框架 后来有企业级的Solr框架 而Elasticsearch框架尤其适合于数据量特别 ...
- 什么是OOA/OOD
Object Oriented Analyzing Object Oriented Design Object Oriented Programming ooa(object oriented ana ...
- Objective-C Log Handling
NSLog method In order to print logs, we use the NSLog method in Objective-C programming language whi ...