题目

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. POJ1274 The Perfect Stall[二分图最大匹配 Hungary]【学习笔记】

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 23911   Accepted: 106 ...

  2. SQL Server Management Studio 教程二: 创建新登录名

    1.先用windows身份登录SQL server2008 2.打开[安全性],右击[登录名],选择[新建登录名] 3.[常规]选项页面中,修改如下位置设置,默认数据库可以是其他数据库,不一定是mas ...

  3. Activator 动态构造对象

    Activator  意义: 用于动态构造对象 语法1: 根据指定的泛型类型构造对象 Activator.CreateInstance<类型>() 语法2: 根据程序集和类型名构造对象 S ...

  4. 【转】topcoder插件配置(傻瓜教程-图文版)

    地址:http://whucc2009luochen.blog.163.com/blog/static/1305715602010827102342860/ 1.插件下载地址:http://www.t ...

  5. 反接保护电路 Reverse Voltage Protection

    Reverse Voltage Protection I've long wanted to pull together some reverse polarity protection ideas ...

  6. CRC32 Source Code

    /* The Quest Operating System * Copyright (C) 2005-2010 Richard West, Boston University * * This pro ...

  7. TeeChart 有用的属性

    //背景 BackWall.Gradient.Visible = True //是否显示右边图标选项 Legend.Visible = False //不在显示3D效果, 比较有用 View3D = ...

  8. [Winform]Cefsharp重写alert与confirm弹窗

    摘要 在使用winform内嵌cefsharp浏览本地页面的时候,如果出现alert弹窗,会在标题栏显示页面所在目录.所以想起来重写alert的样式,通过winform的MessageBox进行提示. ...

  9. 在ASP.NET MVC中使用Knockout实践02,组合View Model成员、Select绑定、通过构造器创建View Model,扩展View Model方法

    本篇体验使用ko.computed(fn)计算.组合View Model成员.Select元素的绑定.使用构造器创建View Model.通过View Model的原型(Prototype)为View ...

  10. libuv

    libuv 1. 概述 libuv是一个支持多平台的异步IO库.它主要是为了node.js而开发的,但是也可以用于Luvit, Julia, pyuv及其他软件. 注意:如果您发现了此软件中的错误,那 ...