LeetCode题解-112-Path Sum

原题


原题链接https://leetcode.com/problems/path-sum/

这里要注意的是路径必须是从root到leaf , 不能是中间一段。


解法概览

解法1:迭代法,深度优先

解法2:别人的更优雅的递归,参考https://discuss.leetcode.com/topic/3149/accepted-my-recursive-solution-in-java

解法3:我的递归,


解法1

思路分析

利用后序遍历,如果到了叶子节点并且路径的sum与给定的sum相同,那么返回true。后序遍历的思路与图解请见:http://blog.csdn.net/wangt443/article/details/51863846

代码

public class Solution112_iterator {
    public boolean hasPathSum(TreeNode root, int sum) {
        int currentSum = 0;
        TreeNode lastVist = null;
        Deque<TreeNode> stack = new LinkedList<TreeNode>();

        if (root != null) {
            stack.push(root);
            currentSum += root.val;
        }

        while (!stack.isEmpty()){
            while (stack.peek() != null){
                TreeNode currentNode = stack.peek();
                stack.push(currentNode.left);
                if(currentNode.left != null)
                    currentSum += currentNode.left.val;
            }
            stack.pop();

            if (!stack.isEmpty()){
                TreeNode currentNode = stack.peek();        //此时已是最左端,如果右子树为空则为叶子节点
                if (currentNode.right == null && currentNode.left == null && currentSum == sum)
                    return true;
                if (currentNode.right == null || currentNode.right == lastVist){
                    lastVist = stack.pop();
                    currentSum -= lastVist.val;
                    stack.push(null);
                }
                else {
                    stack.push(currentNode.right);
                    currentSum += currentNode.right.val;
                }
            }
        }
        return false;
    }
}

解法2

解题思路

每一次子递归都是将sum减去当前的val;

代码

public class Solution112_recursive_2 {
    public boolean hasPathSum(TreeNode root, int sum) {
        if(root == null)
            return false;
        if(root.left == null && root.right == null)
            return sum == root.val;

        return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);
    }
}


解法3

解题思路

由于自己的解法使用的是加法,所以需要引入currentSum这第三个参数,没有解法2的“减法”简介。

代码

public class Solution112_recursive {
    public boolean hasPathSum(TreeNode root, int sum) {
        if (root == null)
            return false;
        else
            return hasPathSumHelper(root, root.val, sum);
    }

    private  boolean hasPathSumHelper(TreeNode root, int currentSum ,int sum){
        boolean lefHasPathSum = false, rightHasPathSum = false;
        if (root == null)
            return false;
        if (root.left == null && root.right == null && currentSum == sum)
            return true;
        if (root.left != null){
            lefHasPathSum = hasPathSumHelper(root.left, currentSum + root.left.val, sum);
        }
        if (root.right != null)
            rightHasPathSum = hasPathSumHelper(root.right, currentSum + root.right.val, sum);

        return lefHasPathSum || rightHasPathSum;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值