【问题标题】:Tree - path sum树 - 路径总和
【发布时间】:2015-10-17 23:33:54
【问题描述】:

问题 -> 给定一棵二叉树和一个总和,确定该树是否具有从根到叶的路径,使得沿该路径的所有值相加等于给定总和。

我的解决方案 ->

public class Solution {
    public boolean hasPathSum(TreeNode root, int sum) {
        if (root == null || sum == 0){
            return false;
        }
        List<Integer> resultSet = new ArrayList<Integer>();
        Integer result = root.val;
        inorder(root, result, resultSet);
        return resultSet.contains(sum);
    }
    public void inorder(TreeNode root, Integer result, List<Integer> resultSet){
        if (root.left == null && root.right == null){
            resultSet.add(result);
        }
        if (root.left != null) {
            result += Integer.valueOf(root.left.val);
            inorder(root.left, result, resultSet);
        }
        if (root.right != null) {
            result += Integer.valueOf(root.right.val);
            inorder(root.right, result, resultSet);
        }

    }
}

输出->

输入: [1,-2,-3,1,3,-2,null,-1] 3 输出:真 预期:错误

我真的不确定我哪里出了问题。我尝试使用 result 的 int 和 Integer 类型选项,但它不起作用。请帮忙。

【问题讨论】:

    标签: java data-structures tree


    【解决方案1】:

    我看到的问题是result 变量,因为一旦您将left 节点的值添加到result 并使用left 子树完成,那么您会将right child 的值添加到结果,这是错误的,因为现在它具有 leftright 子值的总和。

    所以本质上你是在result 中添加之前所有节点的值 inorder遍历中的节点root

    你可以试试这个:

    public void inorder(TreeNode root, Integer result, List<Integer> resultSet){
        if (root.left == null && root.right == null){
            resultSet.add(result);
        }
        if (root.left != null) {
            inorder(root.left, result+Integer.valueOf(root.left.val), resultSet);
        }
        if (root.right != null) {
            inorder(root.right, result+Integer.valueOf(root.right.val), resultSet);
        }
    }
    

    编辑:1

    解决此问题的另一种简单方法:您无需创建包含所有根到叶路径之和的数组。您可以简单地继续减少所需的总和。

    代码:

    public boolean hasPathSum(TreeNode root, int sum) {
        if (root == null) {
            return false;
        } else {
            return hasPathSumHelper(root, sum);
        }
    
    }
    
    boolean hasPathSumHelper(TreeNode root, int sum) {
        if (root.left == null && root.right == null) {//if leaf node
            if (Integer.valueOf(root.val) == sum) { //if node value is equal to sum
                return true;
            } else {
                return false;
            }
        }
        if ((root.left != null) && (root.right != null)) {
            return (hasPathSumHelper(root.left, sum - Integer.valueOf(root.val)) || hasPathSumHelper(root.right, sum - Integer.valueOf(root.val)));
        }
        if (root.left != null) {
            return hasPathSumHelper(root.left, sum - Integer.valueOf(root.val));
        } else {
            return hasPathSumHelper(root.right, sum - Integer.valueOf(root.val));
        }
    }
    

    【讨论】:

    • 嘿,那没用。不,我没有在一级添加左节点和子节点值。我通过递归调用更深一层,然后只添加结果。输入:[7,0,null,-1,-6,null,1,null,null,-7] 0 输出:false 预期:true 所以,本质上我只是在不同级别添加节点值,然后检查是否我是否击中了叶节点。我使用类似的代码来查找树中的不同路径。所以认为这种方法也应该适用于这个问题。但是在某个地方我出错了。
    • 我发布了另一种解决此问题的方法。检查出。你也可以尝试打印出你的结果数组,看看它有哪些不同的路径。这将有助于用您当前的方法调试问题
    • 好的,谢谢!后来我尝试了类似的方法,它奏效了。
    猜你喜欢
    • 2016-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-04
    • 1970-01-01
    相关资源
    最近更新 更多