Path Sum

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.

SOLUTION 1:

相当简单的题目,用递归解决。base case: 如果root是叶子,并且sum为root的值,返回true.
否则在左右分别 找一下就好(调用递归),任何一边返回true都可以的。

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/tree/hasPathSum.java

 public boolean hasPathSum(TreeNode root, int sum) {
if (root == null) {
return false;
} if (root.left == null && root.right == null && sum == root.val) {
return true;
} return hasPathSum(root.left, sum - root.val)
|| hasPathSum(root.right, sum - root.val);
}

LeetCode: Path Sum 解题报告的更多相关文章

  1. LeetCode: Combination Sum 解题报告

    Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...

  2. 【LeetCode】124. Binary Tree Maximum Path Sum 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...

  3. 【LeetCode】112. 路径总和 Path Sum 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 回溯 BFS 栈 日期 题目地址:https ...

  4. LeetCode: Minimum Path Sum 解题报告

    Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to b ...

  5. LeetCode: Binary Tree Maximum Path Sum 解题报告

    Binary Tree Maximum Path SumGiven a binary tree, find the maximum path sum. The path may start and e ...

  6. 【LeetCode】931. Minimum Falling Path Sum 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 相似题目 参考资料 日期 题目地址:htt ...

  7. 【LeetCode】64. Minimum Path Sum 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  8. LeetCode 2 Add Two Sum 解题报告

    LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...

  9. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

随机推荐

  1. Linux下DIR,dirent,stat等结构体详解(转)

    最近在看Linux下文件操作相关章节,遇到了这么几个结构体,被搞的晕乎乎的,今日有空,仔细研究了一下,受益匪浅. 首先说说DIR这一结构体,以下为DIR结构体的定义: struct __dirstre ...

  2. ubuntu下制作u盘启动盘

    ubuntu12.04下成功制作了ubuntu13.10 U盘启动盘. 成功 ubuntu14.04下成功制作了centos.7 U盘启动盘.成功 1.安装u盘制作工具unetbootin sudo ...

  3. android image加载中等待动画

    在布局中添加一个ImageViw和一个EditText. <ImageView android:id="@+id/loading_imageView_info" androi ...

  4. java反射之获取枚举对象

    项目中导入大量枚举对象,用来定义常量.随着带来一个问题,就是每个枚举类都需要通过key来获取对应枚举的需求. public enum ExamType { CRAFT(1, "草稿" ...

  5. JQuery EasyUI Combobox的onChange事件

    html中的select 的change事件 <select id="consult_province" name="consult_province" ...

  6. 实现超级简单的bug管理系统

    大家可以试着去实现一个超级简单的bug管理系统 不需要鉴权,也就是不需要登陆 有tag管理功能,缺陷上可以加tag,通过tag区分bug的状态和类型 bug的增删改查功能 bug描述支持markdow ...

  7. javascript 中的==(相等运算符)与===(等同运算符)比较

    javascript 中的==(相等运算符)与===(等同运算符)比较:(1)==用于一般比较,===用于严格比较,(2)==在比较的时候可以转换数据类型,===严格比较,只要类型不匹配就返回flas ...

  8. php实现rar文件的读取和解压

    PHP Rar Archiving 模块 (php_rar) 是一个读取和解压rar文件的模块,但不提供RAR压缩(打包)的功能. 1.首先要到PECL的RAR页面下载DLL. 根据自己的情况选择下载 ...

  9. QT creator 编辑器快捷键

    QT creator 编辑器快捷键 一.快捷键配置方法:   进入“工具->选项->环境->键盘”即可配置快捷键.     二.常用默认快捷键:       编号 快捷键 功能 1 ...

  10. 如何利用IPv6进行远程桌面连接

    如何利用IPv6进行远程桌面连接 学校是教育网,其中寝室和实验室的IPv4地址被划分成了两个VLAN,所以没法使用windows的远程连接功能.今天突然想到学校的IPv6地址可能并未划分成两个VLAN ...