题目

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.

题解

还是对树的操作,递归的解法:

 1     public boolean hasPathSum(TreeNode root, int sum) {
 2         if(root == null) 
 3             return false;
 4         
 5         sum -= root.val;
 6         if(root.left == null && root.right==null)  
 7             return sum == 0;
 8         else 
 9             return hasPathSum(root.left,sum) || hasPathSum(root.right,sum);
     }

非递归的解法(Reference:http://www.programcreek.com/2013/01/leetcode-path-sum/):

 1     public boolean hasPathSum(TreeNode root, int sum) {
 2         if(root == null) return false;
 3  
 4         LinkedList<TreeNode> nodes = new LinkedList<TreeNode>();
 5         LinkedList<Integer> values = new LinkedList<Integer>();
 6  
 7         nodes.add(root);
 8         values.add(root.val);
 9  
         while(!nodes.isEmpty()){
             TreeNode curr = nodes.poll();
             int sumValue = values.poll();
  
             if(curr.left == null && curr.right == null && sumValue==sum){
                 return true;
             }
  
             if(curr.left != null){
                 nodes.add(curr.left);
                 values.add(sumValue+curr.left.val);
             }
  
             if(curr.right != null){
                 nodes.add(curr.right);
                 values.add(sumValue+curr.right.val);
             }
         }
  
         return false;
     }

Path Sum leetcode java的更多相关文章

  1. 【LeetCode】Path Sum ---------LeetCode java 小结

    Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that addi ...

  2. Binary Tree Maximum Path Sum leetcode java

    题目: Given a binary tree, find the maximum path sum. The path may start and end at any node in the tr ...

  3. Minimum Path Sum leetcode java

    题目: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right w ...

  4. 【LeetCode】Path Sum 2 --java 二叉数 深度遍历,保存路径

    在Path SUm 1中(http://www.cnblogs.com/hitkb/p/4242822.html) 我们采用栈的形式保存路径,每当找到符合的叶子节点,就将栈内元素输出.注意存在多条路径 ...

  5. Binary Tree Maximum Path Sum - LeetCode

    Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence ...

  6. LeetCode算法题-Path Sum(Java实现)

    这是悦乐书的第169次更新,第171篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第28题(顺位题号是112).给定二叉树和整数sum,确定树是否具有根到叶路径,使得沿路 ...

  7. leetcode 113 Path Sum II ----- java

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

  8. Minimum Path Sum [LeetCode]

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  9. Path Sum [LeetCode]

    Problem Description: http://oj.leetcode.com/problems/path-sum/ Pretty easy. /** * Definition for bin ...

随机推荐

  1. WatermarkMaker

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

  2. react的非DOM操作

    非dom属性?dangerouslySetInnerHTML,ref,key非dom标准属性,也就是说dom标准里面没有规定的属性,react引入了三个非dom属性,如上. dangerouslySe ...

  3. 青客宝团队Consul内部分享ppt

    青客宝团队Consul内部分享ppt   https://mp.weixin.qq.com/s?src=3&timestamp=1503647705&ver=1&signatu ...

  4. MongoDB的Java驱动使用整理 (转)

    MongoDB Java Driver 简单操作 一.Java驱动一致性 MongoDB的Java驱动是线程安全的,对于一般的应用,只要一个Mongo实例即可,Mongo有个内置的连接池(池大小默认为 ...

  5. WICED SDK 3.3.1

    7/20/2015 UPDATE: After installing the IDE you may not see all the APPs.  Press F5 in Eclipse to ref ...

  6. oracle 两个逗号分割的字符串 如何判断是否其中有相同值

    比如字段A: 'ab,cd,ef,gh'字段B: 'aa,bb,cc,dd' 没有相同值 字段A: 'ab,cd,ef,gh'字段B: 'aa,bb,cd,dd' 有相同值cd 1.CREATE OR ...

  7. 领域Model?

    前言 领域驱动设计里有很多东西,我们可以应用在各种各样的开发模式里,所以接下来说的一些东西,我们可以部分使用. 说道领域驱动的领域,大家肯定就要开始说Bounded Context,聚合,聚合根,容易 ...

  8. [Go] 编码规范

    gofmt 大部分的格式问题可以通过 gofmt 解决,gofmt 自动格式化代码,保证所有的Go代码与官方推荐的格式保持一致,于是所有格式有关问题,都以 gofmt 的结果为准. 注释 在编码阶段应 ...

  9. 利用 PHP 导出 Git 某个分支下,新增或修改过的文件

    使用 SVN 作为版本控制的时候,整理过一个 导出文件脚本:利用 PHP 导出 SVN 新增或修改过的文件 现在换成了 Git,整理出类似的脚本: [第一版]git.php <?php /** ...

  10. 无法打开物理文件 XXX.mdf",操作系统错误 5:"5(拒绝访问。)"的解决办法

    http://blog.csdn.net/blackfield/article/details/6550499 用T-SQL命令附加数据库时,出现如下异常信息: 无法打开物理文件 XXX.mdf&qu ...