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.


题目标签:Tree

  这道题目给了我们一个二叉树和一个sum, 让我们判断这个二叉树是否有至少一条path 的之和是等于sum的。利用preOrder 来遍历树,每次用sum 减去当前点的值,每当遇到一个leaf node 的时候检查sum 是不是等于0, 返回ture 和false。利用 || 来return 所有的boolean 值, 至少有过一个true,一个path之和等于sum, 总的boolean 就是true。

Java Solution:

Runtime beats 13.93%

完成日期:07/03/2017

关键词:Tree

关键点:当是leaf node 的时候检查sum;利用 || return两个children的返回值

 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution
{
public boolean hasPathSum(TreeNode root, int sum)
{
if(root == null)
return false; sum -= root.val; if(root.left == null && root.right == null)
{
if(sum == 0)
return true;
else
return false;
} return hasPathSum(root.left, sum) || hasPathSum(root.right, sum);
}
}

参考资料:

http://www.cnblogs.com/springfor/p/3879825.html

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

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

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

  3. 112. Path Sum二叉树路径和

    [抄题]: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding ...

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

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

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

  6. leetcode:Path Sum (路径之和) 【面试算法题】

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

  7. LeetCode 112. Path Sum(路径和是否可为sum)

    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] 437. Path Sum III 路径和 III

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

  10. [LeetCode] 113. Path Sum II 路径和 II

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

随机推荐

  1. 从文本中读取内容并把读取到的内容转化成二进制保存的形式(包含十进制数如何转换成二进制数dtob函数)

    #include<stdio.h> #include<string.h> #include<stdlib.h> #include<malloc.h> d ...

  2. python基础之socket

    一.osi七层 完整的计算机系统由硬件,操作系统,软件组成. 互联网的核心就是由一堆协议组成,协议就是标准,如全世界通信的标准就是英语. 如果把计算机比作人,那么互联网协议就是计算机界的英语,所有计算 ...

  3. Activiti第二篇【管理流程定义、执行任务和流程实例、流程变量】

    上篇Activiti只是一个快速入门案例,这篇就讲定义.部署.查看任务等等的一些细节[涉及到的数据库表.对象等等]- 管理流程定义 管理流程定义主要涉及到以下的4张表: -- 流程部署相关的表 SEL ...

  4. Wrong FS: hdfs://......, expected: file:///

    单机版使用的是FileSystem类的静态函数: FileSystem hdfs = FileSystem.get(conf) 伪分布式下需要使用Path来获得 Path path = new Pat ...

  5. 【轉】JS,Jquery获取各种屏幕的宽度和高度

    Javascript: 网页可见区域宽: document.body.clientWidth网页可见区域高: document.body.clientHeight网页可见区域宽: document.b ...

  6. Element-ui Theme浅析

    一.浅析 1.采用BEM方式管理类名 B:block,模块,一个块是一个独立的实体,块可以包含其它块,名字单词间用-连接:如一个搜索块: E:element,元素,一个元素是块的一部分,具有某种功能, ...

  7. 手动添加 Git bash 到鼠标右键

    由于不知原因,右键没有了Git Bash Here,没有这个右键菜单导致获取Git仓库中的代码很不方便,所以决定通过注册表的方式将这个菜单加出来. 1.win + R,输入"regedit& ...

  8. wxPython中菜单、按钮学习

    ---恢复内容开始--- wx.Window 是一个基类,许多构件从它继承.包括 wx.Frame 构件.技术上这意味着,我们可以在所有的 子类中使用 wx.Window 的方法.我们这里介绍它的几种 ...

  9. zoj 1938 Binomial Showdown 组合数裸基础

    Binomial Showdown Time Limit: 2 Seconds      Memory Limit: 65536 KB In how many ways can you choose ...

  10. Spring事务管理配置示例

    (一).Spring事务特性 1.事务隔离级别 隔离级别是指若干个并发的事务之间的隔离程度. ISOLATION_DEFAULT:默认值,使用数据库的默认隔离级别,就是ISOLATION_READ_C ...