题目

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. BZOJ4161 常系数齐次线性递推

    问了数竞的毛毛搞了一番也没太明白,好在代码蛮好写先记下吧. #include<bits/stdc++.h> using namespace std; ,mod=1e9+; int n,k, ...

  2. VM 虚拟机网络配置

    VM网络设置,一共有四种模式. 分别是 1:bridge:桥接,直接和真实网卡相连.如果你要让虚拟机也要上网,就必须选这项,并且要配置和真实网卡在同一网段的IP地址. 2:host-only: 仅主机 ...

  3. VMware 使用本机代理上网

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

  4. allowDefinition='MachineToApplication错误

    配置错误说明: 在处理向该请求提供服务所需的配置文件时出错.请检查下面的特定错误详细信息并适当地修改配置文件. 分析器错误信息: 在应用程序级别之外使用注册为 allowDefinition='Mac ...

  5. Maven学习——修改Maven的本地仓库路径

    安装Maven后我们会在用户目录下发现.m2 文件夹.默认情况下,该文件夹下放置了Maven本地仓库.m2/repository.所有的Maven构件(artifact)都被存储到该仓库中,以方便重用 ...

  6. JTAG - General description of the TAP Controller states

    A transition between the states only occurs on the rising edge of TCK, and each state has a differen ...

  7. Win10系统下如何禁止同步主机session?windows 10禁止同步主机session的方法

    近来,有些刚刚升级Win10正式版的用户反映自己的电脑开机时有个同步主机session启动项占用了将近半分钟,而选择用360禁止后,下次会出现同步主机session3,再禁止下次又会出现同步主机ses ...

  8. 关于GPL协议的理解(开源与商用、免费与收费的理解)

    编者:请特别注意看暗红色粗体标注的那几句话,总结下来有下面几点: 如果你用了我的 GPL软件,那么你的软件也必须要开源,否则就不能使用我的软件,你是否把你的软件商用和我没关系 Oracle 卖的不是软 ...

  9. 得到WAV文件的长度

    long getVideoLength (char *fileName){   MCI_OPEN_PARMS mciOpenParms;   MCI_STATUS_PARMS mciStatusPar ...

  10. ubuntu下安装android sdk运行模拟器出现错误:

    ./emulator: error while loading shared libraries: libstdc++.so.6: cannot open shared object file: No ...