Leetcode 112&113&437(Java)

今天来总结一下Leetcode上目前我做到关于Path Sum即二叉树求和的内容,涉及的题目有三道,题号为112(easy),113(medium),437(easy)。

Leetcode 112

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.

用DFS结合递归来求解这道题目,用sum减去当前结点值,放入下一次递归的sum中,注意题中符合条件的路径是根到叶子结点,因此在判断是要注意叶子结点的判别。本题AC码如下:

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

Leetcode 113

Given a binary tree and a sum, find all root-to-leaf paths where each path’s sum equals the given sum.

For example:
Given the below binary tree and sum = 22,
5
/ \
4 8
/ / \
11 13 4
/ \ / \
7 2 5 1
return
[
[5,4,11,2],
[5,8,4,5]
]

这道题目在112的基础上更进一步,要输出所有符合输出条件的路径。同样用递归,用一个List来存走过的结点,若符合条件,则将这个List存人List中的答案list中:

    public class Solution {
    public List<List<Integer>> pathSum(TreeNode root, int sum){
        List<List<Integer>> result  = new LinkedList<List<Integer>>();
        List<Integer> currentResult  = new LinkedList<Integer>();
        pathSum(root,sum,currentResult,result);
        return result;
    }

    public void pathSum(TreeNode root, int sum, List<Integer> currentResult,
            List<List<Integer>> result) {

        if (root == null)
            return;
        currentResult.add(new Integer(root.val));
        if (root.left == null && root.right == null && sum == root.val) {
            result.add(new LinkedList(currentResult));
            currentResult.remove(currentResult.size() - 1);
            return;
        } else {
            pathSum(root.left, sum - root.val, currentResult, result);
            pathSum(root.right, sum - root.val, currentResult, result);
        }
        currentResult.remove(currentResult.size() - 1);
    }
}

Leetcode 437

这道题去掉了叶子结点的要求,返回了答案的个数而非详细内容。

public class Solution {
    public int pathSum(TreeNode root, int sum) {
        List<Integer> currentResult  = new LinkedList<Integer>();
        int result = 0;
        pathSum(root,sum,currentResult,result);
        return result;
    }


    public void pathSum(TreeNode root, int sum, List<Integer> currentResult,
            int result) {

        if (root == null)
            return;
        currentResult.add(new Integer(root.val));
        if (sum == root.val) {
            result++;
            currentResult.remove(currentResult.size() - 1);
            return;
        } else {
            pathSum(root.left, sum - root.val, currentResult, result);
            pathSum(root.right, sum - root.val, currentResult, result);
        }
        currentResult.remove(currentResult.size() - 1);
    }

}

总结

刚开始刷Leetcode的时候,对树的理解还是比较偏理论,多做些类似的题脑子里的思路会越来越清晰,希望等题量累积到足够量的时候可以自己来总结一篇树问题的求解SOP~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值