题目

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. 【10.31校内测试】【组合数学】【记忆化搜索/DP】【多起点多终点二进制拆位Spfa】

    Solution 注意取模!!! Code #include<bits/stdc++.h> #define mod 1000000007 #define LL long long usin ...

  2. 区别ES3ES5和ES6this的指向问题。区分普通函数和箭头函数中this的指向问题

    ES3 ES5this的指向问题 this指的是该函数被调用的对象 var foo = function () { this.a = 'a', this.b = 'b', this.c = { a: ...

  3. LabTool : LPC LINK2, LPC4370 cheap scope: 80Ms/s 12 bit

    80MHz 12 bit ADC processor LPC4370.LPCxpresso do a LPC LINK2 and LABTOOLS open source oscilloscope d ...

  4. IAR EWARM __iar_program_start, __iar_data_init3, __iar_copy_init3, __iar_zero_init3

    #include <stdint.h> // The type of a pointer into the init table. typedef void const * table_p ...

  5. 如何用visio(word)绘制图片表格

    1.用visio是插入excel表格,但是不能差如公示了,修改的话也是进入了excel修改. 2.在word里修改即可,word表格可以插入公式,然后阿银玉兰或者转给pdf截图就好

  6. 怎样连接REDIS服务端

    怎样连接REDIS服务端 REDIS服务器是TCP/IP SERVER,因此客户端要访问,必须先同服务器建立SOCKET连接,然后才可以发送各种REDIS COMMAND(指令). 首先要引用单元文件 ...

  7. Ora-01536:超出了表空间users的空间限量

      正在开会,同事跑过来说数据库有问题,通讯程序不能入库,赶快获取一条insert into a values()语句后在toad工具中手动插入,发现报错:Ora-01536:超出了表空间users的 ...

  8. javascript游戏引擎

    基于JavaScript开发的游戏是唯一一个能够跨桌面,Web和移动三种平台的.今天,本文向大家推荐一些非常棒的JavaScript游戏开发框架. AD:干货来了,不要等!WOT2015 北京站演讲P ...

  9. localhost与127.0.0.1及本机ip的区别

    很多人会接触到这个ip地址127.0.0.1.也许你会问127.0.0.1是什么地址?其实127.0.0.1是一个回送地址,指本地机,一般用来测试使用.大家常用来ping 127.0.0.1来看本地i ...

  10. 破产姐妹第一季/全集2 Broke Girls迅雷下载

    本季2 Broke Girls Season 1 (2011)看点:黑发泼辣的Max(凯特·戴琳斯 Kat Dennings 饰)在纽约布鲁克林区一家低档餐馆打工,餐馆同事包括小个子亚裔老板Han L ...