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. (Easy)

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.

分析:

还是用递归的思路,可以把sum - root -> val用在递归函数中,这样写最简洁。

代码:

 class Solution {
public:
bool hasPathSum(TreeNode* root, int sum) {
if(root == nullptr) {
return false;
}
if(root -> right == nullptr && root -> left == nullptr) {
return sum == root->val;
}
return hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root->val) ;
}
};

LeetCode112 Path Sum的更多相关文章

  1. 第34-1题:LeetCode112. Path Sum I

    题目 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和. 说明: 叶子节点是指没有子节点的节点. 示例:  给定如下二叉树,以及目标和 sum ...

  2. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  3. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  4. [LeetCode] Path Sum III 二叉树的路径和之三

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  5. [LeetCode] Binary Tree Maximum Path Sum 求二叉树的最大路径和

    Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...

  6. [LeetCode] Path Sum II 二叉树路径之和之二

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  7. [LeetCode] Path Sum 二叉树的路径和

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

  8. Path Sum II

    Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...

  9. Path Sum

    需如下树节点求和 5  /  \ 4     8  /     /  \ 11  13    4 / \     /  \  7    2      5   1 JavaScript实现 window ...

随机推荐

  1. 关于在静态html中实现语言切换的思路与实现

    在项目中只用到了三种语言:英文.中文简体.中文繁体.所以我首先想到了最笨的方法:1.直接将三种语言写在html中,显示当前设置的语言隐藏其它两种来实现.2.使用css伪元素的content:attr( ...

  2. jquery源码学习(四)—— jquery.extend()

    a.jQuery.extend( source ) b.jQuery.extend(destination, source1, source2, source3 ....) c.jQuery.exte ...

  3. 【笔记】LR中设置检查点

      我们为什么需要在LR中设置检查点?? 我们在录制编写脚本后,通常会进行回放,如果回放通过没有错误.我们就认为脚本是正确的.那么LR怎么区分脚本是否回放正确:基本上所有脚本回放错误都是因为 404错 ...

  4. springmvc 使用poi解析excel并通过hibernate连续插入多条数据 实际数据库只能保存最后一条

    有一个原始数据的excel表 用poi解析之后通过hibernate插数据库 结果 后来发现,有人说 果断尝试 问题解决 但是这好像并不是真正解决问题,只是解决了一个现象 因为有人说 https:// ...

  5. Hibernate: insert into xxx (name) values (?)但是数据库中没有数据

    学习hibernate 控制台提示 但数据库中没有任何数据被插入 同样的代码,参考例程中就有数据被插入 比较无解,删除部分代码,红框中的部分,运行一下,再贴回去,就好了

  6. python基础--线程、进程

    并发编程: 操作系统:(基于单核研究) 多道技术: 1.空间上的复用 多个程序共用一个计算机 2.时间上的复用 切换+保存状态 例如:洗衣 烧水 做饭 切换: 1.程序遇到IO操作系统会立刻剥夺着CP ...

  7. centos下彻底删除mysql

    打算重新试试安装两个mysql,就把老的删除了. yum remove mysql mysql-server mysql-libs compat-mysql51 rm -rf /var/lib/mys ...

  8. Python实例 遍历文件夹和文件

    import  os import  os.path #  os,os.path里包含大多数文件访问的函数,所以要先引入它们. #  请按照你的实际情况修改这个路径 rootdir  =   &quo ...

  9. 集训队日常训练20180513-DIV1

    A.3132 给一个有向图,问能否从任意点出发都能进入一个环中. 深搜. #include<bits/stdc++.h> using namespace std; ; vector< ...

  10. Leetcode633.Sum of Square Numbers平方数之和

    给定一个非负整数 c ,你要判断是否存在两个整数 a 和 b,使得 a2 + b2 = c. 示例1: 输入: 5 输出: True 解释: 1 * 1 + 2 * 2 = 5 示例2: 输入: 3 ...