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.


题解:其实很类似这道题:http://www.cnblogs.com/sunshineatnoon/p/3853376.html,也是用递归的方法,在每个root上计算一个sum,表示从树根节点到当前节点得到的和,然后判断当前节点是否是叶节点,如果是,再判断从根节点到当前节点路径上的和是否等于sum,是就找到了要求的路径。

代码如下:

 /**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
private boolean hadPath = false;
private void hasPathDfs(TreeNode root,int sum,int currSum){
if(root == null)
return;
currSum = currSum + root.val;
if(root.left == null && root.right == null && currSum == sum){
hadPath = true;
return;
}
hasPathDfs(root.left, sum, currSum);
hasPathDfs(root.right, sum, currSum);
}
public boolean hasPathSum(TreeNode root, int sum) {
hasPathDfs(root, sum, 0);
return hadPath;
}
}

【leetcode刷题笔记】Path Sum的更多相关文章

  1. 【leetcode刷题笔记】Sum Root to Leaf Numbers

    Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...

  2. LeetCode刷题笔记和想法(C++)

    主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...

  3. 18.9.10 LeetCode刷题笔记

    本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...

  4. LeetCode 刷题笔记 (树)

    1.  minimum-depth-of-binary-tree 题目描述 Given a binary tree, find its minimum depth.The minimum depth ...

  5. LeetCode刷题笔记 - 12. 整数转罗马数字

    学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...

  6. 【leetcode刷题笔记】Triangle

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  7. Leetcode刷题笔记(双指针)

    1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...

  8. LeetCode 刷题笔记 1. 两数之和(Two Sum)

    tag: 栈(stack) 题目描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案. ...

  9. (python)leetcode刷题笔记 01 TWO SUM

    1. Two Sum Given an array of integers, return indices of the two numbers such that they add up to a ...

随机推荐

  1. easyui首页模板

    Easyui首页html代码 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "htt ...

  2. StarUML破解教程

    StarUML破解教程 StarUML官方下载地址:http://staruml.io/download StarUML是一个非常好用的画UML图的工具,但是它是收费软件,以下是破解方法: 1.使用E ...

  3. 旋转卡壳求两个凸包最近距离poj3608

    #include <iostream> #include <cmath> #include <vector> #include <string.h> # ...

  4. Sitemesh3的使用心得

    项目中用到了sitemesh3,就把使用心得记下来,至于配置之类的,官方网站都有,这里只是写下自己对它的理解,方便再次理解, sitemesh是基于过滤器的原理,拦截到符合配置文件中配置的路径,然后会 ...

  5. 我的Android进阶之旅------>关于android:layout_weight属性的一个面试题

    最近碰到一个面试题,按照下图,由Button和EditText组成的界面下厨布局代码,解决这题目需要使用android:layout_weight的知识. 首先分析上图所示的界面可以看成一下3个部分. ...

  6. 股票技术指标中的VOL,KDJ,MACD,OBV,VR,DMA分别代表什么意思?很关键,谢谢

    http://zhidao.baidu.com/link?url=glKK7n0JUgqgrvfx2Gzd937-5zZg1bC615MwAp0P_mrYDytnMUpjoOQgYU871ny8St1 ...

  7. Floyd 学习笔记

    #include <cstdio> #include <cstring> #include <ctype.h> #include <cstdlib> # ...

  8. IOS蓝牙开发模块

    一.引言 蓝牙是设备近距离通信的一种方便手段,在iPhone引入蓝牙4.0后,设备之间的通讯变得更加简单.相关的蓝牙操作由专门的 CoreBluetooth.framework进行统一管理.通过蓝牙进 ...

  9. LintCode:链表操作(合并与反转)

    描述: (1)翻转一个链表 样例 给出一个链表1->2->3->null,这个翻转后的链表为3->2->1->null ********************** ...

  10. spring AOP简单实现代码存放

    @Before:使用Before增强处理只能在目标方法执行之前织入增强,如果Before增强处理没有特殊处理,目标方法总会自动执行,如果Before处需要阻止目标方法的执行,可通过抛出一个异常来实现. ...