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.

思路:DFS.时间复杂度O(n), 空间复杂度O(lgN)

相关题目:《剑指offer》面试题25

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

[LeetCode] PathSum的更多相关文章

  1. leetcode — path-sum

    /** * Source : https://oj.leetcode.com/problems/path-sum/ * * * Given a binary tree and a sum, deter ...

  2. Leetcode::Pathsum & Pathsum II

    Pathsum Description: Given a binary tree and a sum, determine if the tree has a root-to-leaf path su ...

  3. 递归 & 分治算法深度理解

    首先简单阐述一下递归,分治算法,动态规划,贪心算法这几个东西的区别和联系,心里有个印象就好. 递归是一种编程技巧,一种解决问题的思维方式:分治算法和动态规划很大程度上是递归思想基础上的(虽然实现动态规 ...

  4. leetcode 38:path-sum

    题目描述 给定一个二叉树和一个值sum,判断是否有从根节点到叶子节点的节点值之和等于sum的路径, 例如: 给出如下的二叉树,sum=22,              5              / ...

  5. path-sum leetcode C++

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

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

  7. [LeetCode] 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] 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 ...

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

随机推荐

  1. drag element

    <div id="logDiv" draggable="true" style="border: 2px dotted red; width: ...

  2. Linux入门第二天——基本命令入门(下)

    一.帮助命令 1.帮助命令:man (是manual手册的缩写,男人无所不能,/笑哭) 更多man用法以及man page的用法,参见:http://www.linuxidc.com/Linux/20 ...

  3. SaltStack入门篇(七)之架构部署实战

    模块:https://docs.saltstack.com/en/2016.11/ref/states/all/index.html 实战架构图: 实验环境设置: 主机名 IP地址 角色 linux- ...

  4. underscore.js 分析 第三天

    // Create a safe reference to the Underscore object for use below. // 为Underscore对象创建一个安全的引用 // _为一个 ...

  5. python实现socket通信

    python实现socket很简单,保证你的环境有响应的python环境就可以,我使用的是socket,demo代码如下: server端程序: # coding:utf-8 import socke ...

  6. C#反射的简单示例

    反射(Reflection)可以在运行时获 得.NET中每一个类型(包括类.结构.委托.接口和枚举等)的成员,包括方法.属性.事件,以及构造函数等.还可以获得每个成员的名称.限定符和参数等反正说白了就 ...

  7. CentOS 6.5关闭防火墙

    关闭命令:  service iptables stop 永久关闭防火墙:chkconfig iptables off 两个命令同时运行,运行完成后查看防火墙关闭状态 service iptables ...

  8. 强化学习读书笔记 - 09 - on-policy预测的近似方法

    强化学习读书笔记 - 09 - on-policy预测的近似方法 参照 Reinforcement Learning: An Introduction, Richard S. Sutton and A ...

  9. EOJ3650 转机折扣(26进制,字符串)

    题面 看成26进制,把较小的那个字符串加1 strcmp(s1,s2)s1和s2有大小时,不一定都是返回1或者-1.....这个地方wa了好几次没有发现 #include<bits/stdc++ ...

  10. python编辑购物车

    一.需求分析 输入工资金额,进入购物车,并打印输出商品编号和价格,用户可以通过输入商品编号进行商品选购 余额不足时,打印提示信息 通过q进行退出结算 购物车能够循环购物 二.代码实现 ShoopCar ...