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. JournalNode的作用

    NameNode之间共享数据(NFS .Quorum Journal Node(用得多)) 两个NameNode为了数据同步,会通过一组称作JournalNodes的独立进程进行相互通信.当activ ...

  2. go-byte数组最大的长度

    本来想打算用go来处理一个1G左右的txt文本的,但是在去读取的时候就报内存溢出了,提示数组已经无法在分配. 用的是:ioutil.ReadFile 方法来读取文本,它的返回值是一个[]byte 数组 ...

  3. 20155311高梓云补交的Mypc课下实践

    20155311高梓云补交的Mypc课下实践 老师,由于我自己的疏忽导致没有及时交上这次作业.这是我的代码和截图. ``` ```/**import java.io.; import java.lan ...

  4. 20155316 实验三《敏捷开发与XP实践》实验报告

    实验1 实验内容 在IDEA中使用工具(Code->Reformate Code)把下面代码重新格式化,再研究一下Code菜单,找出一项让自己感觉最好用的功能.提交截图,加上自己学号水印. pu ...

  5. [BZOJ1857][SCOI2010]传送带-[三分]

    Description 传送门 Solution 三分套三分.代码简单但是证明苦兮兮.. 假如我们在AB上选了一个点G,求到该点到D的最小时间. 图中b与CD垂直.设目前从G到D所耗时间最短的路径为G ...

  6. thinkphp5 开启多语言

    一.配置点击打开链接1.开启语言包功能'lang_switch_on' => true,2.支持的语言列表'lang_list' => ['zh-cn','en-us'],二.语言定义(默 ...

  7. jenkins+jacoco+ant自动化代码和应用服务代码分离场景获取远程服务的覆盖率

    前提 自动化代码和应用服务代码分离.jenkins和tomcat服务器分离 思想 1.在tomcat启动javaagent监听. 2.运用其他job_B已部署的应用服务代码 3.拉取自动化代码,开始测 ...

  8. [.NET] 使用HttpClient操作HFS (HTTP File Server)

    前言 本篇文章介绍如何使用HttpClient操作HFS (HTTP File Server),为自己留个纪录也希望能帮助到有需要的开发人员.关于HTTP File Server的介绍.安装.设定,可 ...

  9. 记Thinkpad的一次扩容升级经历

    俗话说:" 工欲善其事,必先利其器" 阅读目录: 背景 目标 准备 友情提示 制作引导盘 分区及备份 拆机装盘 重装系统 写在结束的 参考资料 背景: 作为一个近六年的IT从业Co ...

  10. windows下Mysql安装启动及常用操作

    1.下载mysql https://dev.mysql.com/downloads/ 2.配置环境变量 变量名:MYSQL_HOME 变量值:E:\MySql\mysql-8.0.15-winx64\ ...