题目

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. java 使用grpc步骤

    1.配置grpc maven依赖 <dependency> <groupId>io.grpc</groupId> <artifactId>grpc-ne ...

  2. iml文件

    iml是 intellij idea的工程配置文件,里面是当前projec的一些配置信息 ==== android studio  svn 汉化 常规. 描述.网络.安全的shell  壳. 用命令行 ...

  3. 在JavaScript中什么时候使用==是正确的?

    在JavaScript中什么情况下使用==是正确的?简而言之:没有.这篇文章来看五种情况下总是使用===,并且解释为什么不用==. JavaScript有两种操作符用来比较两个值是否相等 [1]: 严 ...

  4. VMware 使用本机代理上网

    灰机使用方法 VMware 安装方法 首先解决主机的配置 1.查询本机 IP 地址,使用 ipconfig /all 2.更改小灰机的设置 3.虚拟机设置 4.Ubuntu 设置

  5. java php c# 三种语言的AES加密互转

    java php c# 三种语言的AES加密互转 最近做的项目中有一个领取优惠券的功能,项目是用php写得,不得不佩服,php自带的方法简洁而又方便好用.项目是为平台为其他公司发放优惠券,结果很囧的是 ...

  6. 【转】InitializingBean的作用

    原文链接:http://blog.csdn.net/maclaren001/article/details/37039749 最近工作需要得到sping中的每个事物需要执行的sql,称机会简单研究了一 ...

  7. nginx php-fpm安装配置(转)

    nginx本身不能处理PHP,它只是个web服务器,当接收到请求后,如果是php请求,则发给php解释器处理,并把结果返回给客户端. nginx一般是把请求发fastcgi管理进程处理,fascgi管 ...

  8. How to detect the types of executable files

    How to detect the types of executable files type { IMAGE_DOS_HEADER: DOS .EXE header. } IMAGE_DOS_HE ...

  9. Golang Vendor 包管理工具 glide 使用教程

    Glide 是 Golang 的 Vendor 包管理器,方便你管理 vendor 和 verdor 包.类似 Java 的 Maven,PHP 的 Composer. Github:https:// ...

  10. bash 脚本中分号的作用

    在Linux bash shell中,语句中的分号一般用作代码块标识 1.单行语句一般要用到分号来区分代码块.比如: weblogic@pmtest:/$if [ "$PS1" ] ...