zoukankan      html  css  js  c++  java
  • 112. 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.

    Hide Tags
     Tree Depth-first Search 

    链接:http://leetcode.com/problems/path-sum/ 

    一刷,DFS

    class Solution(object):
        def hasPathSum(self, root, sum):
            if not root:
                return False
            if not root.left and not root.right and sum == root.val:
                return True
            return self.hasPathSum(root.left, sum - root.val) or self.hasPathSum(root.right, sum - root.val)

    2/17/2017, Java, performance only bit 2%...

    用类似前序遍历的方法。一定要注意什么时候需要pop,每次入栈的都是什么值。

    注意叶节点是在stack的peek上,空节点不在。

    recursive方法远好于iterative算法

     1 public class Solution {
     2     public boolean hasPathSum(TreeNode root, int sum) {
     3         if (root == null) return false;
     4         TreeNode node = root;
     5         TreeNode previous = null;
     6         Stack<TreeNode> stack = new Stack<>();
     7         int value = sum;
     8         stack.push(root);
     9         value -= root.val;
    10 
    11         while (!stack.isEmpty()) {
    12             node = stack.peek();
    13             if (previous == null || previous.left == node || previous.right == node) {
    14                 if (node.left == null && node.right == null) {
    15                      if (value == 0) return true;
    16                      else {
    17                          previous = stack.pop();
    18                          value += previous.val;
    19                      }
    20                 } else if (node.left != null) {
    21                     stack.push(node.left);
    22                     value -= node.left.val;
    23                     previous = node;
    24                 } else if (node.right != null) {
    25                     stack.push(node.right);
    26                     value -= node.right.val;
    27                     previous = node;
    28                 }
    29             } else if (previous == node.left) {
    30                 if (node.right != null) {
    31                     stack.push(node.right);
    32                     value -= node.right.val;
    33                     previous = node;
    34                 } else {
    35                     previous = stack.pop();
    36                     value += previous.val;
    37                 }
    38 
    39             } else if (previous == node.right) {
    40                 previous = stack.pop();
    41                 value += previous.val;
    42             }
    43         }
    44         return false;
    45     }
    46 }

    5/8/2017

    算法班

    1 public class Solution {
    2     public boolean hasPathSum(TreeNode root, int sum) {
    3         if (root == null) return false;
    4         if (root.left == null && root.right == null && root.val == sum) return true;
    5         return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);
    6     }
    7 }
  • 相关阅读:
    RSA加密
    各种正则
    用Fragment制作的Tab页面产生的UI重叠问题
    Android源码下载
    Android Studio使用百度地图问题总结
    Android获取网络类型
    Android Studio类中实现Serializable自动生成serialVersionUID
    【Android开发】如何设计开发一款Android App
    UIViewController生命周期
    微信支付开发经验分享
  • 原文地址:https://www.cnblogs.com/panini/p/5609870.html
Copyright © 2011-2022 走看看