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. DS博客作业06--图

    1.本周学习总结 1.1.思维导图 1.2.谈谈你对图结构的认识及学习体会 本章学习了图结构的相关知识,图形结构属于复杂的非线性数据结构,在实际应用中很多问题可以用图来描述.在图结构中,每个元素可以有 ...

  2. Etcd和ZooKeeper,究竟谁在watch的功能表现更好?

    ZooKeeper和Etcd的主要异同可以参考这篇文章,此外,Etcd的官网上也有对比表格(https://coreos.com/etcd/docs/latest/learning/why.html) ...

  3. HDU——1215七夕节(因数和)

    七夕节 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submis ...

  4. 洛谷P4363 [九省联考2018]一双木棋chess 【状压dp】

    题目 菲菲和牛牛在一块n 行m 列的棋盘上下棋,菲菲执黑棋先手,牛牛执白棋后手. 棋局开始时,棋盘上没有任何棋子,两人轮流在格子上落子,直到填满棋盘时结束. 落子的规则是:一个格子可以落子当且仅当这个 ...

  5. java面试题之什么是CAS

    CAS,即Compare and Switch,比较-替换,里面有三个操作数:内存值V.旧的预期值A.要修改的值B: 当预期值A和内存值V相同时,才会将内存值修改为B并返回true,否则什么都不做并返 ...

  6. SQL的主键和外键和唯一约束

    SQL的主键和外键的作用: 外键取值规则:空值或参照的主键值. (1)插入非空值时,如果主键表中没有这个值,则不能插入. (2)更新时,不能改为主键表中没有的值. (3)删除主键表记录时,你可以在建外 ...

  7. webpack打包字体图标报错的解决办法

    webpack打包字体图标需要两个加载器  url-loader 和 file-loader 另外  字体图标的引入方式  本来应该是  url("....") 这样的方式,但是w ...

  8. 在vue单页面应用当中使用sass

    之前在项目当中有使用过sass,但是使用的方式有点Low,是在vue文件当中的style下面通过@import的方式引入的. 其实在webpack当中也可以通过在main.js当中import &qu ...

  9. 不支持模块化规范的插件可以使用import 导入的原因

    模块化当中的模块其实是个闭包,然后导出这个闭包,这个是为了解决全局变量污染的问题的. 所以模块当中直接定义的变量 比如  var foo = 0; 这个并不会是全局变量,而是当前模块闭包当中的局部变量 ...

  10. 微信小程序中 input组件影响页面样式的问题

    input组件有个默认的宽高,好像是不能清除的,在使用flex布局的时候,发现会影响到页面的布局,以为是flex布局的问题,改为float布局试了下也是同样的问题,试着把input标签换成别的标签,问 ...