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. Markdown常用语法学习

    Markdown常用语法学习,这些就够用了. 演示地址: https://github.com/YalongYan/Markdown-- 特别提示:  标题'##'后面必须加一个空格,否则编译不对.# ...

  2. 第三方-Swift2.0后Alamofire的使用方法

    第一部分,配置项目 首先我们创建一个工程如下图 在此只讲纯手打拉第三方框架的方法 然后把下载的Alamofire解压文件全部放进创建的项目文件夹中,如下图 关键时刻到了哦,集中精神,注意!!! 这个图 ...

  3. LINQ分页工具

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Co ...

  4. O-Bomb(数位dp)

    Bomb Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submi ...

  5. hdu 4068 I-number【大数】

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=4608 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  6. Django 之ModelForm

    1.Form表单的回顾 Model - 数据库操作 - 验证 class A(MOdel): user = email = pwd = Form - class LoginForm(Form): em ...

  7. centos7 Authentication failure

    root@localhost ~]#su bash-4.2$ su Password: su: Authentication failure //这里切换的是系统用户,现在还不清楚为什么postgre ...

  8. 华为机试ACM(字符组合问题)

    今晚做了华为的机试,3道ACM题,最后一道是实现从M个不同字符中任取N个字符的所有组合. eg: input:ABC 2 output:AB AC BC 第一个输入为字符串,第二个输入为组合的字符个数 ...

  9. activiti基础--2----------------------(流程定义)

    Deployment 部署对象 1.一次部署的多个文件信息,对于不需要的流程可以删除和修改 2.对应的表 act_re_deployment #部署对象表 act_re_procdef #流程定义表 ...

  10. (转)Spring 缓存EhCacheFactoryBean

    Spring使用Cache 从3.1开始,Spring引入了对Cache的支持.其使用方法和原理都类似于Spring对事务管理的支持.Spring Cache是作用在方法上的,其核心思想是这样的:当我 ...