【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 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的更多相关文章
- 【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 ...
- LeetCode刷题笔记和想法(C++)
主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...
- 18.9.10 LeetCode刷题笔记
本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...
- LeetCode 刷题笔记 (树)
1. minimum-depth-of-binary-tree 题目描述 Given a binary tree, find its minimum depth.The minimum depth ...
- LeetCode刷题笔记 - 12. 整数转罗马数字
学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...
- 【leetcode刷题笔记】Triangle
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- Leetcode刷题笔记(双指针)
1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...
- LeetCode 刷题笔记 1. 两数之和(Two Sum)
tag: 栈(stack) 题目描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案. ...
- (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 ...
随机推荐
- thinkPHP5.0的学习研究【基础】
2017年6月19日13:25:56 基础:1.ThinkPHP5的环境要求如下: PHP >= 5.4.0 PDO PHP Extension MBstring P ...
- iptables的扩展,layer7实现
l7filter:介绍 能过滤7层协议,例如过滤QQ.迅雷 原版只支持2.6.28,不支持centos6的2.6.32:马哥的版本支持2.6.32 实现l7filter: 给内核(netfilter) ...
- codeforces Gravity Flip 题解
版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/,未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...
- 中国移动OnetNet云平台 GET指令使用
GET /devices//datastreams/KEY HTTP/1.1 Host: api.heclouds.com api-key: pmWLtnkDBSreKfvg7GsClUXdXa4A ...
- PHP eval函数使用介绍
eval()函数中的eval是evaluate的简称,这个函数的作用就是把一段字符串当作PHP语句来执行. 复制代码代码如下: eval("echo'hello world';") ...
- linux查看当前文件夹下每个文件大小
查看当前文件夹下每个文件大小 并会给出当前文件大小总和,后面加具体的文件名会显示具体的文件大小 ls -lht 把*换成具体的文件名会显示具体的文件大小 du -sh *
- Chrome Extension 扩展程序 小白入门
Chrome Extension 扩展程序 前请说明:本文适用于之前从来没有接触过chrome extension扩展程序的同学~ 编写demo 创建项目文件夹chrome_ext_demo,在项目根 ...
- php正则表达式和数组
一.正则表达式 1. “/”代表定界符,"^"代表起始符号,"$"代表结束符号 $str1="abc123def45ghjk6789lou" ...
- 3.22课·········CSS样式表
CSS(Cascading Style Sheet,叠层样式表),作用是美化HTML网页. /*注释区域*/ 此为注释语法 一.样式表 (一)样式表的分类 1.内联样式表 和HTML联合显示,控 ...
- ubuntu14.04 在自带python2.7上安装python3.3.5 可以用但是有问题
一开始写的时候并没有发现这么安装有问题,后来发现问题也不想删了,当个教训,如果想安装从python自带版本换别的版本的话就别接着看了,这么安装有问题.需要进行配置,但是我还不会.其实下面只是差了一步配 ...