[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 ...
随机推荐
- 【Go】windows下搭建go语言编译环境
主要是协助杨哥做Kubernetes相关工作,由于Kubernetes和Docker都是由Go语言编写,因此改源码后还是需要go语言编译器来编译运行.所以打算先在windows上安装一下go语言环境. ...
- 【POJ 2186】Popular Cows
http://poj.org/problem?id=2186 tarjan求强连通分量. 因为SD省选用WinXP+Cena评测而且不开栈,所以dfs只好写手动栈了. 写手动栈时思路清晰一点应该是不会 ...
- BZOJ 2631 tree(动态树)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2631 [题目大意] 要求支持链修改,链查询,边断开,连接操作 [题解] 链修改分乘和加 ...
- SQL 中 HAVING 用法
现在 Student表有 如下数据 现需求如下: 查找每个老师的学生的平均年龄且平均年齿大于12 不使用 HAVING SELECT * FROM (SELECT TeacherID, AVG(Age ...
- Problem E: 深入浅出学算法019-求n的阶乘
Problem E: 深入浅出学算法019-求n的阶乘 Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 5077 Solved: 3148 Descrip ...
- Problem C: 调用函数,求a+aa+aaa+....+aa...aa(n个a)
#include <stdio.h> int fn(int a,int n)//定义函数 { ; ;i<=n;i++) { m=m+a;//当a=3时,m=3,然后a=30,m=33 ...
- codevs 1779 单词的划分
1779 单词的划分 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description Problem有一个很长的由小写字母组成字符串.为了便于对 ...
- [转]为什么匿名内部类参数必须为final类型
1) 从程序设计语言的理论上:局部内部类(即:定义在方法中的内部类),由于本身就是在方法内部(可出现在形式参数定义处或者方法体处),因而访问方法中的局部变量(形式参数或局部变量)是天经地义的.是很自 ...
- “Warning: Call-time pass-by-reference has been deprecated in”解决方法
刚刚在调试一个PHP木马,显示错误信息为: Warning: Call-time pass-by-reference has been deprecated in E:\New-Hack520org\ ...
- 通过Chrome模拟和调试网速慢的情况来限制一些P2P视频网站上传速度占满的情况