Leetcode 112&113&437(Java)

来源:互联网 发布:glov卸妆巾 知乎 编辑:程序博客网 时间:2024/06/04 19:25

今天来总结一下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
原创粉丝点击