[leetcode]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,
5
/ \
4 8
/ / \
11 13 4
/ \ \
7 2 1
return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.
分析:
题目是要看给定的树中是否存在一条从根到叶节点的路径,该路径上全部节点上值的和相加为给定的值。能够採用递归的思想,每次看该节点的右子树节点的值是否为当前sum减去当前节点的值。左子树也进行相同的处理。
代码:
class Solution {
public:
bool hasPathSum(TreeNode* root, int sum) {
if(!root)
return false;
if(!root->left&&!root->right)
{
if(root->val==sum)
return true;
else
return false;
}
return hasPathSum(root->left,sum-root->val)||hasPathSum(root->right,sum-root->val);
}
};
[leetcode]Path Sum--巧用递归的更多相关文章
- LeetCode:Path Sum I II
LeetCode:Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such ...
- [LeetCode] Path Sum III 二叉树的路径和之三
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] 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] Path Sum II 二叉树路径之和之二
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- [leetcode]Path Sum II
Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...
- LeetCode: Path Sum II 解题报告
Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...
- [LeetCode] Path Sum IV 二叉树的路径和之四
If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...
- LeetCode: Path Sum 解题报告
Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that addi ...
- LeetCode Path Sum IV
原题链接在这里:https://leetcode.com/problems/path-sum-iv/description/ 题目: If the depth of a tree is smaller ...
- [Leetcode] Path Sum II路径和
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
随机推荐
- 洛谷——P2095 营养膳食
题目描述 Mr.L正在完成自己的增肥计划. 为了增肥,Mr.L希望吃到更多的脂肪.然而也不能只吃高脂肪食品,那样的话就会导致缺少其他营养.Mr.L通过研究发现:真正的营养膳食规定某类食品不宜一次性吃超 ...
- FZU - 1492(Problem 1492 地震预测)
怀特先生是一名研究地震的科学家,最近他发现如果知道某一段时间内的地壳震动能量采样的最小波动值之和,可以有效地预测大地震的发生. 假设已知一段时间的n次地壳震动能量的采样值为a1,a2,-an,那么第i ...
- BZOJ 2705 [SDOI2012]Longge的问题(欧拉函数)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2705 [题目大意] 求出∑gcd(i,N)(1<=i<=N) [题解] $ ...
- 【动态规划】【滚动数组】Educational Codeforces Round 26 D. Round Subset
给你n个数,让你任选K个,使得它们乘起来以后结尾的0最多. 将每个数的因子2和因子5的数量求出来,记作a[i]和b[i]. 答案就是max{ min{Σa[i],Σb[i]} }(a[i],b[i]是 ...
- 【最大流】POJ3236-ACM Computer Factory
[题意] 装配一个电脑需要P个零件,现在给出N机器的信息,每个机器可以将k个电脑由状态{S1,S2..,Sp}转变为{Q1,Q2..,Qp},问最多能装配多少台电脑以及对应的方案? [思路] 1A.. ...
- python基础之组合继承多态
组合 1.什么是组合 组合就是一个类的对象具备一个指向另外一个类的对象的属性 2.为何用组合 组合可以减少代码冗余 3.如何使用 class People: def __init__(self,nam ...
- Alpha冲刺(6/10)——追光的人
1.队友信息 队员学号 队员博客 221600219 小墨 https://www.cnblogs.com/hengyumo/ 221600240 真·大能猫 https://www.cnblogs. ...
- #iOS问题记录#UITableView加载后直接滑动倒最底部
类似QQ的聊天框,当进入聊天框,直接滑动倒最底部: 需要先将以他变了view滚动倒底部,再来移动NSIndexPath, 代码如下: -(void) doForceScrollToBottom { d ...
- Maven命名规范收集
一.基本命名规范: groupId:定义当前Maven项目隶属的实际项目,例如org.sonatype.nexus,此id前半部分org.sonatype代表此项目隶属的组织或公司,后部分代表项目的名 ...
- 手机在线更新系统MySQL数据库服务器参数优化mycnf,16G内存8核CPU,
业务场景: 后台支持手机在线更新系统,db服务器内存16G,8核,dell的pc服务器. qps: 200个左右 tps: 1个左右 一分钟50几个 sort_buffer_size = 32M 大了 ...