zoukankan      html  css  js  c++  java
  • Path Sum 解答

    Question

    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.

    Solution 1 -- Recursion

     1 /**
     2  * Definition for a binary tree node.
     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         if (root == null)
    13             return false;
    14         int total = 0;
    15         return dfs(root, sum, total);
    16     }
    17     
    18     private boolean dfs(TreeNode root, int target, int prevSum) {
    19         if (root == null)
    20             return false;
    21         int currentSum = prevSum + root.val;
    22         if (root.left == null && root.right == null) {
    23             return currentSum == target;
    24         } else {
    25             return (dfs(root.left, target, currentSum) || dfs(root.right, target, currentSum));
    26         }
    27             
    28     }
    29 }

    Solution 2 -- BFS

    We can use two stacks here. One to record tree nodes. And the other to record current path sum from root to this node. Since we traverse the tree level by level. It's BFS.

     1 /**
     2  * Definition for a binary tree node.
     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         if (root == null)
    13             return false;
    14         Stack<TreeNode> stack1 = new Stack<TreeNode>();
    15         Stack<Integer> stack2 = new Stack<Integer>();
    16         stack1.push(root);
    17         stack2.push(0);
    18         while (!stack1.empty()) {
    19             TreeNode currentNode = stack1.pop();
    20             int currentSum = stack2.pop() + currentNode.val;
    21             if (currentNode.left == null && currentNode.right == null && currentSum == sum)
    22                 return true;
    23             if (currentNode.left != null) {
    24                 stack1.push(currentNode.left);
    25                 stack2.push(currentSum);
    26             }
    27             if (currentNode.right != null) {
    28                 stack1.push(currentNode.right);
    29                 stack2.push(currentSum);
    30             }
    31         }
    32         return false;
    33     }
    34 }
  • 相关阅读:
    当教育成为一种商品
    怎样设置Solaris上网
    对象转为xml输出到页面,中文乱码问题
    Flex 深拷贝与浅拷贝笔记
    使用access数据库需要注意的问题
    根据数据库表结构生成xsd文件
    SendKeys.Send()输入中文
    VB6迁移到VB.NET的一些问题汇总
    技术文章转移完毕
    说说重复发明轮子的事儿
  • 原文地址:https://www.cnblogs.com/ireneyanglan/p/4850494.html
Copyright © 2011-2022 走看看