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的更多相关文章

  1. [array] leetcode - 40. Combination Sum II - Medium

    leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...

  2. [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 ...

  3. [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 ...

  4. [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 ...

  5. [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 ...

  6. [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 ...

  7. 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 ...

  8. [leetcode]40. Combination Sum II组合之和之二

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  9. [LeetCode] 113. Path Sum II ☆☆☆(二叉树所有路径和等于给定的数)

    LeetCode 二叉树路径问题 Path SUM(①②③)总结 Path Sum II leetcode java 描述 Given a binary tree and a sum, find al ...

随机推荐

  1. HDU——1799循环多少次(杨辉三角/动态规划/C(m,n)组合数)

    循环多少次? Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  2. [CODEVS1917] 深海机器人问题(最小费用最大流)

    传送门 [问题分析] 最大费用最大流问题. [建模方法] 把网格中每个位置抽象成网络中一个节点,建立附加源S汇T. 1.对于每个顶点i,j为i东边或南边相邻的一个节点,连接节点i与节点j一条容量为1, ...

  3. 学习struts2及MVC

    1.MVC模式基础 1.1.MVC模式简介 MVC是一种架构型模式,它本身并不引入新的功能,只是用来指导我们改善应用程序的架构,使得应用的模型和视图相分离,从而达到更好的开发和维护效率.在MVC模式中 ...

  4. visual studio NuGet 常用包管理命令

    visual studio NuGet 常用包管理命令 查找包 Find-Package [包名] Find-Package [包名] -AllVersions 安装包 Install-Package ...

  5. Red is good(bzoj 1419)

    Description 桌面上有R张红牌和B张黑牌,随机打乱顺序后放在桌面上,开始一张一张地翻牌,翻到红牌得到1美元,黑牌则付出1美元.可以随时停止翻牌,在最优策略下平均能得到多少钱. Input 一 ...

  6. 【BZOJ1101】Zap(莫比乌斯反演)

    题意:多组询问,对于给定的整数a,b和d,有多少正整数对x,y,满足x<=a,y<=b,并且gcd(x,y)=d. T,a,b,d,x,y<=50000 思路:下底函数分块+积性函数 ...

  7. 【Visual Studio】Error: forget to add '#include "stdafx.h"' to your source (转)

    原文转自 http://www.cnblogs.com/qunews/articles/2200313.html [问题原因]在编译时使用了预编译头文件, [解决方法]Project-->Pro ...

  8. 呕心沥血之作:完美解决Informix的中文乱码问题

    Informix是IBM旗下的一款数据库,要不是这个项目需要,估计这辈子我都不知道居然还有这么一款数据库.想来公司的项目遍布全国各地,各种部署环境各种应用场景应有尽有,七七八八的问了一大堆的各项目组兄 ...

  9. hdu 2841(容斥原理+状态压缩)

    Visible Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  10. LeetCode OJ——Word Ladder

    http://oj.leetcode.com/problems/word-ladder/ 图的最短路径问题,可以用最短路径算法,也可以深搜,也可以广搜. 深搜版本: 第一次写的时候,把sum和visi ...