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.

Solution1:递归pre-order dfs来downwards累积

Time Complexity: O(N) Space Complexity: O(N) 递归缓存

Solution2:Interative stack pre-order dfs,并downwards累积

累积的方式a: 将cur_sum更新到node的值中,share stack
累积的方式b: 若node的值不可变,可再建一个cur_sum的stack;或组pair共存
Time Complexity: O(N) Space Complexity: O(N)

Solution3:Interative stack 法二, 纪录sum & step back

则不用存sum进stack
Time Complexity: O(N) Space Complexity: O(N)

Solution1 Code:

class Solution {
    public boolean hasPathSum(TreeNode root, int sum) {
        if(root == null) return false;
        
        if(root.left == null && root.right == null && root.val == sum) return true;
        
        return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);
    }
}

Solution2a Code:

class Solution2a {
    public boolean hasPathSum(TreeNode root, int sum) {
        if(root == null) return false;
        Stack<TreeNode> stack = new Stack<>();
            
        stack.push(root);
        while(!stack.isEmpty()) {
            TreeNode cur = stack.pop();
            if(cur.left == null && cur.right == null && cur.val == sum) return true;
                
            if(cur.right != null) {
                cur.right.val += cur.val;
                stack.push(cur.right);
            }
                    
            if(cur.left != null) {
                cur.left.val += cur.val;
                stack.push(cur.left);
            }
        }
        return false;
    }
}

Solution2b Code:

class Solution2b {
    public boolean hasPathSum(TreeNode root, int sum) {
        if(root == null) return false;
        Stack<TreeNode> stack = new Stack<>();
        Stack<Integer> stack_sum = new Stack<>();
            
        stack.push(root);
        stack_sum.push(root.val);
        while(!stack.isEmpty()) {
            TreeNode cur = stack.pop();
            int cur_sum = stack_sum.pop();
            if(cur.left == null && cur.right == null && cur_sum == sum) return true;
                
            if(cur.right != null) {
                stack.push(cur.right);
                stack_sum.push(cur_sum + cur.right.val);
            }
                    
            if(cur.left != null) {
                stack.push(cur.left);
                stack_sum.push(cur_sum + cur.left.val);
            }
        }
        return false;
    }
}

Solution3 Code:

class Solution3 {
    public boolean hasPathSum(TreeNode root, int sum) {
        // Deque<TreeNode> stack = new ArrayDeque<>();
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        while(!stack.isEmpty() || cur != null) {
            while(cur != null) {
                stack.push(cur);
                
                // process
                sum -= cur.val;
                if(cur.left == null && cur.right == null && sum == 0) return true;
                
                cur = cur.left;
            } 
            
            // pop and sum_step_back togeter, but
            // only do that after finishing processing its right_siblings,
            // otherwise when visiting right_siblings, its root will be wrongly sum_step_back.
            while (!stack.isEmpty() && stack.peek().right == cur) {
                cur = stack.pop();
                sum += cur.val;  // sum: step back
            }
            cur = stack.isEmpty() ? null : stack.peek().right;
        }
        return false;
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 14,350评论 0 33
  • 生命中,我们还有很多时间去实现自己梦想,我们脚下的路还是那么的坚实,阳光依旧那么的灿烂,父母尚且还在,这种日子其实...
    夜雨轻尘阅读 1,781评论 0 0
  • 为自己健康做定投,身体是自己的,钱没有可以赚。 当身体出现各种疾病,有钱不一定好用。 每天坚持快走10公里,风雨无...
    一个落魄老男人阅读 5,594评论 0 5
  • 2015-09-23 18:1920 人,还是要善良,即使生活再不易。 ​ 在后街像微微复印店,牛牛饰品店,阿波罗...
    包小包ya阅读 3,424评论 0 0
  • 起床啦,跟着巧虎学英语,现在的动作都是跟着巧虎学的,怎么感觉每天起床的造型又够搞人 上午把我多年珍藏拿出来给她糟蹋...
    文娟_e9e9阅读 2,394评论 0 0