leetCode(40):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.
搜索支路的方法
/**
* 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 findPath(TreeNode* root, int nowSum,const int sum)
{
nowSum += root->val;//当前值 //假设已经是叶子结点,且和相等,则直接返回真
if (!root->left && !root->right && nowSum == sum)
return true; bool found_left = false;
bool found_right = false; if (root->left) //左子树不为空。则在左子树中搜索
found_left = findPath(root->left, nowSum, sum); if (!found_left && root->right) //假设左子树没找到,则在右子树中搜索
found_right = findPath(root->right, nowSum, sum); return found_left || found_right;//仅仅要在一条支中上找到则返回真
} bool hasPathSum(TreeNode* root, int sum) {
if (root == NULL)
return false;
findPath(root, 0, sum);
}
};
leetCode(40):Path Sum的更多相关文章
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- [LeetCode] 437. Path Sum III_ Easy tag: DFS
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [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 ...
- [LeetCode] 113. Path Sum II 路径和 II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- [LeetCode] 437. Path Sum III 路径和 III
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] 666. Path Sum IV 二叉树的路径和 IV
If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...
- LeetCode 437. Path Sum III (路径之和之三)
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [leetcode]40. Combination Sum II组合之和之二
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- [LeetCode] 113. Path Sum II ☆☆☆(二叉树所有路径和等于给定的数)
LeetCode 二叉树路径问题 Path SUM(①②③)总结 Path Sum II leetcode java 描述 Given a binary tree and a sum, find al ...
随机推荐
- [Pandas技巧] 如何把pandas dataframe对象或series对象转换成list
import pandas as pd >>> df = pd.DataFrame({'a':[1,3,5,7,4,5,6,4,7,8,9], 'b':[3,5,6,2,4,6,7, ...
- curl保存图片
$url = 'http://p1.qhimg.com/t013dfc89f8a039122c.jpg?size=690x460'; function http_get_data($url) { $c ...
- oracle报错处理
oracle安装过程报错 报错一:Error in invoking target 'install' of makefile '/u01/app/oracle/product/11.2.0/dbho ...
- HDU——2093考试排名(string类及其函数的运用以及istringstream)
考试排名 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- iOS-BMK标注&覆盖物
在iOS开发中,地图算是一个比较重要的模块.我们常用的地图有高德地图,百度地图,谷歌地图,对于中国而言,苹果公司已经不再使用谷歌地图,官方使用的是高德地图.下面将讲述一下百度地图开发过程中的一些小的知 ...
- Android2.2源码init机制分析
1 源码分析必备知识 1.1 linux内核链表 Linux内核链表的核心思想是:在用户自定义的结构A中声明list_head类型的成员p,这样每个结构类型为A的变量a中,都拥有同样的成员p,如下: ...
- 谷歌Chrome 27测试版已经发布 更快的浏览速度
谷歌已经发布了Chrome 27测试版浏览器,为普通用户带来了大约提升为5%的网页浏览速度.用户已经可以在Chrome测试网页下载到最新的更新了.我们已经对这版更新做了一个全面的快速测试,让我们看看究 ...
- 【CF725D】Contest Balloons(贪心,堆)
题意:acm队伍可以得气球,相同气球数是一个排名.每个队伍有一个气球数上限,如果该队伍的气球数大于上限 该队伍被淘汰.给了你队伍的气球数,你的气球可以给别人,问你最大可能的排名. (2 ≤ n ≤ 3 ...
- hdu 4311 & 4312 Meeting point 曼哈顿距离之和最小
hdu 4311 题意 平面上\(n(n\leq 1e5)\)个点,找一个点到其它所有点的曼哈顿距离之和最小. 思路 如果是找一个坐标使得所有点到其曼哈顿距离之和最小,那么将\(n\)个横坐标排个序, ...
- poj 1410 Intersection 线段相交
题目链接 题意 判断线段和矩形是否有交点(矩形的范围是四条边及内部). 思路 判断线段和矩形的四条边有无交点 && 线段是否在矩形内. 注意第二个条件. Code #include & ...