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.

Note: A leaf is a node with no children.

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。

可以用递归,emmm,关键是找出递归方法

大佬的讲解如下:

这道求二叉树的路径需要用深度优先算法DFS的思想来遍历每一条完整的路径,也就是利用递归不停找子节点的左右子节点,而调用递归函数的参数只有当前节点和sum值。首先,如果输入的是一个空节点,则直接返回false,如果如果输入的只有一个根节点,则比较当前根节点的值和参数sum值是否相同,若相同,返回true,否则false。 这个条件也是递归的终止条件。下面我们就要开始递归了,由于函数的返回值是Ture/False,我们可以同时两个方向一起递归,中间用或||连接,只要有一个是True,整个结果就是True。递归左右节点时,这时候的sum值应该是原sum值减去当前节点的值。

链接:http://www.cnblogs.com/grandyang/p/4036961.html

C++代码:

/**
* 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 hasPathSum(TreeNode* root, int sum) {
if(!root)
return false;
int cur = root->val;
if(cur == sum && !root->left && !root->right) return true;
return hasPathSum(root->left,sum - cur)|hasPathSum(root->right,sum-cur);
}
};

(二叉树 DFS 递归) leetcode 112. Path Sum的更多相关文章

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

  2. leetcode 112. Path Sum 、 113. Path Sum II 、437. Path Sum III

    112. Path Sum 自己的一个错误写法: class Solution { public: bool hasPathSum(TreeNode* root, int sum) { if(root ...

  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] 112. Path Sum ☆(二叉树是否有一条路径的sum等于给定的数)

    Path Sum leetcode java 描述 Given a binary tree and a sum, determine if the tree has a root-to-leaf pa ...

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

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

  7. LeetCode 112. Path Sum 二叉树的路径和 C++

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

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

  9. LeetCode 112 Path Sum(路径和)(BT、DP)(*)

    翻译 给定一个二叉树root和一个和sum, 决定这个树是否存在一条从根到叶子的路径使得沿路全部节点的和等于给定的sum. 比如: 给定例如以下二叉树和sum=22. 5 / \ 4 8 / / \ ...

随机推荐

  1. 在MongoDB中创建一个索引而性能提升1000倍的小例子

    在https://www.cnblogs.com/xuliuzai/p/9965229.html的博文中我们介绍了MongoDB的常见索引的创建语法.部分同学还想看看MongoDB的威力到底有多大,所 ...

  2. python 迭代器协议和生成器

    一.什么是迭代器协议 1.迭代器协议是指:对象必须提供一个next方法,执行该方法要么返回迭代中的下一项,要么就引起一个stoplteration异常,以终止迭代(只能往后走,不能往前退) 2.可迭代 ...

  3. sqlserver中批量导出所有作业或链接脚本

    问题描述: 经常在数据库迁移到另外一台服务器的时候,需要把作业也迁移过去,但是作业有时候好多个,要是一个个编写监本出来很麻烦 今天知道个简单方法批量可以导出sql脚本,顺便做个笔记 解决方法: 1.在 ...

  4. Vue学习之路1-集成环境安装

    1.前言 Vue 是一款友好的.多用途且高性能的javascript框架,与其它大型框架不同的是,Vue 被设计为可以自底向上逐层应用,它能够帮你创建可维护性和可测试性更强的代码库,Vue是渐进式的j ...

  5. some settings for spacemacs golang

    spacemacs 中的 golang配置 spacemacs 中的 golang layer 已经有很多默认的配置了, 但是都是针对在 GOPATH 下的配置. 如果你的项目不再默认 的 GOPAT ...

  6. Eclipse编程中免除alt+斜杠,设置自动提示

    用eclipse进行编程时,设置自动提示 .abcdefghijklmnopqrstuvwxyz@

  7. POJ3686 The Windy's

    嘟嘟嘟 刚做费用流,思路完全不对呀-- 应该这么想(应该说敢这么想):这道题的关键在于怎么体现这个玩具是第几个加工的,只有这才能求出他的加工时间(因为加工时间包括等待时间). 但等待时间不好求,因此要 ...

  8. Bootstrap开发框架界面的调整处理

    我在之前介绍了很多关于Boostrap的框架方面的文章,主要是介绍各种插件的使用居多,不过有时候觉得基于Metronic的Boostrap框架的界面效果不够紧凑,希望对它进行一定的调整,那么我们应该如 ...

  9. c#, AOP动态代理实现动态权限控制(一)

    因最近工作需要一个动态的权限配置功能,具体实现逻辑是c#的动态代理功能,废话不多说,直接干货.需求: 用户分为管理员.普通用户 不同用户拥有不同功能权限 用户的权限可配置 新增功能时,不用修改权限配置 ...

  10. 分享Winform datagridview 动态生成中文HeaderText

    缘起 很久以前给datagridview绑定列的时候都是手动的,记得以前用Display自定义属性来动态给datagridview绑定列.后来发现不行,于是还在博问发了问题: 后来热心网友帮我回答了这 ...