【问题标题】:Algorithm to print the total sum of all the path打印所有路径总和的算法
【发布时间】:2015-03-15 20:27:56
【问题描述】:

给你一棵二叉树,定义如下:

public class TreeNode {
    public int val;
    public TreeNode left, right;

    public TreeNode(int val) {
        this.val = val;
        this.left = this.right = null;
    }
}

打印所有路径的总和,路径定义为从根节点到任意叶节点的线

例如:

4 5 6 7 8 10 # # # # 9

应该返回:

路径1:4 + 5 + 7

路径2:4 + 5 + 8 + 9

路径3:4 + 6 + 10

所以总路径总和应该是:path1 + path2 + path3

我们如何通过使用来解决问题

  • 递归
  • 非递归

我已经找到了使用非递归的解决方案,但是关于递归方法有一些小问题

非递归方法:

/**
 * non-recursive
 */
public static int pathSum2(TreeNode root) {
    int sum = 0;
    if (root == null) {
        return sum;
    }
    Queue<TreeNode> queueNode = new LinkedList<TreeNode>();
    Queue<Integer> queueSum = new LinkedList<Integer>();

    queueNode.add(root);
    queueSum.add(root.val);
    while (!queueNode.isEmpty()) {
        TreeNode node = queueNode.poll();
        int temp = queueSum.poll();
        if (node.left == null && node.right == null) {
            sum += temp;
        }
        if (node.left != null) {
            queueNode.add(node.left);
            queueSum.add(node.left.val + temp);
        }
        if (node.right != null) {
            queueNode.add(node.right);
            queueSum.add(node.right.val + temp);
        }
    }
    return sum;
}

递归方法:

/**
 * recursive
 */
public List<List<Integer>> pathSum(TreeNode root) {
    List<List<Integer>> rst = new ArrayList<List<Integer>>();

    helper(rst, new ArrayList<Integer>(), root);
    return rst;
}

public void helper(List<List<Integer>> rst, ArrayList<Integer> list,
                   TreeNode root) {
    if (root == null) {
        return;
    }
    list.add(root.val);
    if (root.left == null && root.right == null) {
        rst.add(new ArrayList<Integer>(list));
    }
    helper(rst, list, root.left);
    helper(rst, list, root.right);
    list.remove(list.size() - 1);
}

但是这样一来,我输出的就是所有的路径,如果我想得到这些路径的总和呢?

获得答案的一种方法是迭代 List> 并获得答案,但我认为它效率低下。我们如何仅在 helper() 方法中处理这个问题,因为总而言之,java 只是 pass-by-value

【问题讨论】:

  • 你试过什么?如果人们能看到你到目前为止所做的尝试,特别是你正在努力解决的问题的哪一部分,那么他们会更容易帮助你
  • 闻起来像作业题
  • 只是给你一个粗略的指导,bc 我真的相信这是一个任务:只需按预定顺序遍历树,每次处理一个节点时,你都可以将它的值添加到 List&lt;Integer&gt;通过引用传递给递归函数或将其声明为实例/全局变量。如果您正在处理叶节点:打印列表。如果你“离开”一个节点:从列表中删除它的值。
  • @br3w5 我已经添加了关于递归方法的代码,但我对如何提高效率有点困惑。

标签: algorithm recursion binary-tree non-recursive


【解决方案1】:

我找到了使用递归的解决方案

/**
 * recursive
 */
public List<List<Integer>> pathSum(TreeNode root, int sum) {
    List<List<Integer>> rst = new ArrayList<List<Integer>>();

    helper(rst, new ArrayList<Integer>(), root, sum);
    return rst;
}

public void helper(List<List<Integer>> rst, ArrayList<Integer> list,
                   TreeNode root, int sum) {
    if (root == null) {
        return;
    }
    list.add(root.val);
    if (root.left == null && root.right == null) {
        rst.add(new ArrayList<Integer>(list));
    }
    helper(rst, list, root.left, sum - root.val);
    helper(rst, list, root.right, sum - root.val);
    list.remove(list.size() - 1);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-04
    • 1970-01-01
    • 2021-06-11
    • 1970-01-01
    • 1970-01-01
    • 2023-02-12
    • 2014-08-28
    • 1970-01-01
    相关资源
    最近更新 更多