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. java 8:I / O 基础

    原文地址:https://docs.oracle.com/javase/tutorial/essential/io/index.html 说明:每一个点都有一篇详细的文章与之对应,每翻译完一篇文章会更 ...

  2. java jstl标签

    转自:http://blog.csdn.net/liushuijinger/article/details/9143793 JSTL(JSP Standard Tag Library ,JSP标准标签 ...

  3. java面试题之select、poll和epoll的区别

    消息传递方式: select:内核需要将消息传递到用户空间,需要内核的拷贝动作: poll:同上: epoll:通过内核和用户空间共享一块内存来实现,性能较高: 文件句柄剧增后带来的IO效率问题: s ...

  4. 头脑王者pk答题小程序开发思路 微信pk答题小程序开发 PK答题游戏你也可以做 微信pk答题游戏

    想必大家最近的朋友圈和微信群里都被头脑王者PK答题刷屏了吧.确实很好玩,尤其是2018年的这波风口,手机答题,大家掏出手机,创建一个好友PK,然后你的好友点击进来就可以和你一起PK答题.比之前的游戏好 ...

  5. python根据文件目录、文件类型和文件与当前时间差删除文件

    直接贴代码: 删除某个目录下的文件,不遍历木路下文件夹下的文件,根据时间差删除,默认7天 #!/usr/bin/python # -*- coding: gbk -*- import os impor ...

  6. 富文本编辑器quill---vue组件(vue-quill-editor)的使用

    1.配置webpack plugin 解决以下报错 Uncaught TypeError: Cannot read property 'imports' of undefined (image-res ...

  7. Yii CDbCriteria的常用方法总结

    查看代码   打印 01 $criteria=new CDbCriteria; 02 $criteria->addCondition("id=1");//查询条件,即wher ...

  8. 遍历删除List中的元素,会报错?

    经常会碰到遍历集合,然后删除里面的对象报错, 纠结半天, 百度了一下,有大神说不能用for-each,  for , 只能用迭代器,真的吗?  我就删成功了呢,看代码,请大神们指正! public s ...

  9. 一维数组解最长上升公共子序列(LCIS)

    #include<bits/stdc++.h> using namespace std; + ; int n,a[maxn],b[maxn],dp[maxn]; int main() { ...

  10. HDU 5733 tetrahedron(计算几何)

    题目链接 tetrahedron 题目大意 输入一个四面体求其内心,若不存在内心则输出"O O O O" 解题思路 其实这道题思路很简单,只要类推一下三角形内心公式就可以了. 至于 ...