[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 ...
随机推荐
- PLSQL的注释技巧
概述 这里提供一些注释的技巧,用来模仿Java中的文档注释的功能. 在Eclipse中,鼠标悬浮在类或其成员上,会显示相关的文档注释:在PL/SQL中也有类似的功能,我们需要掌握一些注释技巧,让其可读 ...
- 洛谷——P1190 接水问题
P1190 接水问题 题目描述 学校里有一个水房,水房里一共装有 m 个龙头可供同学们打开水,每个龙头每秒钟的供水量相等,均为 1. 现在有 n 名同学准备接水,他们的初始接水顺序已经确定.将这些同学 ...
- 洛谷——P2813 母舰
P2813 母舰 题目背景 广东汕头聿怀初中 Train#3 Problem 1 (有没有红警既视感~) 题目描述 在小A的星际大战游戏中,一艘强力的母舰往往决定了一场战争的胜负.一艘母舰的攻击力是普 ...
- 关于 CommonJS AMD CMD UMD
1. CommonJS CommonJS 原来叫 ServerJS, 是服务器端模块的规范,Node.js采用了这个规范. 根据CommonJS规范,一个单独的文件就是一个模块.加载模块使用requi ...
- mtk预装apk 方案公司内置预装apk
mtk预装apk 方案公司内置预装apk 韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha == MTK 预知第三方的APK 流程_yua ...
- JDBC 编程工具包
工具包结构 工具包代码 BeanCountHander.java import java.sql.ResultSet; public class BeanCountHander implements ...
- 2017 icpc 南宁网络赛
2000年台湾大专题...英语阅读输入输出专场..我只能说很强势.. M. Frequent Subsets Problem The frequent subset problem is define ...
- 【数学期望】hdu5984 Pocky
http://www.oyohyee.com/post/HDU/5984.html 看这篇吧,懒得写了. 训练时推得的式子有点鬼畜. #include<cstdio> #include&l ...
- 找出N个数中最小的k个数问题(复杂度O(N*logk))
这是一个经典的算法题,下面给出的算法都在给定的数组基础上进行,好处时不用分配新的空间,坏处是会破坏原有的数组,可以自己分配新的空间以避免对原有数组的破坏. 思路一 先直接排序,再取排序后数据的前k个数 ...
- WPF的UI虚拟化
许多时候,我们的界面上会呈现大量的数据,如包含数千条记录的表格或包含数百张照片的相册.由于呈现UI是一件开销比较大的动作,一次性呈现数百张照片就目前的电脑性能来说是需要占用大量内存和时间的.因此需要对 ...