zoukankan      html  css  js  c++  java
  • leetcode -- Path Sum

    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         // Note: The Solution object is instantiated only once and is reused by each test case.
     3         if(root == null){
     4             return false;
     5         }
     6         
     7         if(root.left == null && root.right == null && root.val == sum){
     8             return true;
     9         } else {
    10             return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);
    11         }
    12     }
     1 /**
     2  * Definition for binary tree
     3  * public class TreeNode {
     4  *     int val;
     5  *     TreeNode left;
     6  *     TreeNode right;
     7  *     TreeNode(int x) { val = x; }
     8  * }
     9  */
    10 public class Solution {
    11     public boolean hasPathSum(TreeNode root, int sum) {
    12         // Start typing your Java solution below
    13         // DO NOT write main() function
    14         if(root == null){
    15             return false;
    16         }
    17         ArrayList<ArrayList<Integer>> result = pathSum(root, sum);
    18         if(result.size() != 0){
    19             return true;
    20         }
    21         return false;
    22     }
    23     
    24     public ArrayList<ArrayList<Integer>> pathSum(TreeNode root, int sum) {
    25         // Start typing your Java solution below
    26         // DO NOT write main() function
    27         ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
    28         ArrayList<Integer> seqs = new ArrayList<Integer>();
    29         int curSum = 0;
    30         
    31         generatePathSum(root, result, seqs, curSum, sum);
    32         return result;
    33     }
    34     
    35     public void generatePathSum(TreeNode root, ArrayList<ArrayList<Integer>> result,
    36         ArrayList<Integer> seqs, int curSum, int expectedSum){
    37             curSum += root.val;
    38             seqs.add(root.val);
    39             
    40             boolean isLeaf = root.left == null && root.right == null;
    41             if(curSum == expectedSum && isLeaf){
    42                 ArrayList<Integer> tmp = new ArrayList<Integer>();
    43                 tmp.addAll(seqs);
    44                 result.add(tmp);
    45             }
    46             
    47             if(root.left != null){
    48                 generatePathSum(root.left, result, seqs, curSum, expectedSum);
    49             }
    50             
    51             if(root.right != null){
    52                 generatePathSum(root.right, result, seqs, curSum, expectedSum);
    53             }
    54             
    55             seqs.remove(seqs.size() - 1);
    56             
    57         }
    58 }
     1 public class Solution {
     2     public boolean hasPathSum(TreeNode root, int sum) {
     3         if(root == null){
     4             return false;
     5         }
     6         return checkPath(root, 0, sum);
     7     }
     8     
     9     public Boolean checkPath(TreeNode root, int curSum, int expectedSum) {
    10         curSum += root.val;
    11         boolean isLeaf = root.left == null && root.right == null;
    12         
    13         if(curSum == expectedSum && isLeaf){
    14             return true;
    15         }
    16         boolean lResult = false;
    17         if(root.left != null){
    18             lResult = checkPath(root.left, curSum, expectedSum);
    19         }
    20         boolean rResult = false;
    21         if(root.right != null){
    22             rResult = checkPath(root.right, curSum, expectedSum);
    23         }
    24         
    25         if(lResult || rResult){
    26             return true;
    27         }
    28         
    29         return false;
    30     }
    31 }
  • 相关阅读:
    YUI(YUIcompressor)压缩参数选项
    js进制转换两则
    软件代码生成工具软工厂V2.0版本上线!欢迎新老用户免费使用!
    软件代码自动化生成工具我们该不该用!
    软件代码生成工具软工厂V2.0版本免费使用地址+教学视频,快速完成开发任务。
    转发在.NET上使用ZeroMQ
    . Net环境下消息队列(MSMQ)对象的应用
    消息队列软件产品大比拼
    ubuntu服务器安装指南
    简单的分布式应用程序日志记录器(logger)-基于MSMQ(消息队列)
  • 原文地址:https://www.cnblogs.com/feiling/p/3250862.html
Copyright © 2011-2022 走看看