【问题标题】:Binary Tree: Node frequency count for 0, 1, or 2 children二叉树:0、1 或 2 个子节点的节点频率计数
【发布时间】:2020-05-24 16:34:36
【问题描述】:

我有一个任务:

给定二叉树 T 的根节点。我们区分 T 中的 3 种节点:具有 0 个子节点的节点、具有 1 个子节点的节点和具有 2 个子节点的节点。为每种类型确定 T 中的节点数。将结果作为长度为 3 的整数数组返回。

我得到了一个 Java 文件,它为此算法生成随机测试用例。

我只被允许创建一个函数来完成所有这些。我不允许将任何其他参数传递给下面的方法。我也不允许在我创建的函数之外进行任何其他修改。

在文件中,已经插入了一个基本案例。我被告知使用递归以后序遍历树。

我知道我的代码当前存在问题。但我不知道如何修复它们。

我目前的代码如下:

private static int[] problem1(Node root) {

    int[] arr = new int[3];

    if (root == null) {
        return new int[] {
            -1, // nodes with 0 children
            -1, // nodes with 1 child
            -1 // nodes with 2 children
        };
    }
    //problem1(root.left);
    //problem1(root.right);

    if (root.left != null && root.right != null) {
        arr[2]++;
        problem1(root.left);
        problem1(root.right);
    } else if (root.left != null && root.right == null) {
        arr[1]++;
        problem1(root.left);
    } else if (root.left == null && root.right != null) {
        arr[1]++;
        problem1(root.right);
    } else {
        arr[0]++;
    }
    return arr;
}

Node类定义为:

static class Node {
            public int value;
            public Node left;
            public Node right;
        }

【问题讨论】:

  • 首先,您是否允许返回在您的方法所属的类中声明的静态变量?您的方法是否严格应该只返回一个本地数组?你还介意分享Node 类的定义吗?我们需要更多背景信息!
  • @AmalK 我已将节点类定义添加到我的帖子中。只要我不修改函数之外的任何内容,但它必须是一个 int 数组。
  • 当您的问题得到解答后,请不要在标题中添加“已解决”之类的内容。接受答案是表明您的问题已解决的方式。

标签: java algorithm binary-tree


【解决方案1】:
class Playground {
    public static void main(String[ ] args) {
        Node root = new Node(1, new Node(2, null, null), new Node(3, null, null));
        int[] counts = problem1(root);
        System.out.println(counts[0]); // 2 node(s) with 0 children
        System.out.println(counts[1]); // 0 node(s) with 1 children
        System.out.println(counts[2]); // 1 node(s) with 2 children
    }

    // recursively count number of childs for each root/node. Post-order 
    // traversing means both left and right node will be traversed before
    // any computations.
    public static int[] problem1(Node root) {
        // always need a base-case to stop recursive call.
        if(root == null) {
            return new int[]{0,0,0};
        }

        // post-order traversing
        int[] leftChildCounts = problem1(root.left);
        int[] rightChildCounts = problem1(root.right);

        int [] counts = new int[]{0,0,0};
        counts[0] = leftChildCounts[0] + rightChildCounts[0];
        counts[1] = leftChildCounts[1] + rightChildCounts[1];
        counts[2] = leftChildCounts[2] + rightChildCounts[2];

        if(root.left == null && root.right == null) {
            counts[0]++;
        } else if(root.left != null && root.right != null) {
            counts[2]++;
        } else {
            counts[1]++;
        }        
        return counts;
    }
}

public class Node {
    int value;
    Node left;
    Node right;

    public Node(int value, Node left, Node right) {
        this.value = 0; 
        this.left = left;
        this.right = right;
    }       
}

【讨论】:

  • 我忘了说我只能将节点作为传递给函数的唯一参数。该函数必须返回一个整数数组。
  • @Cyber​​knightMk1 那么你的教授允许任何全局变量吗? ;)
  • @Cyber​​knightMk1 好的,我已经根据您的限制更新了我的答案。
  • 成功了,我之前制作了多个数组,但我从未将它们设置为等于递归调用。非常感谢。
【解决方案2】:

我认为您已经非常接近了,您唯一需要解决的就是处理结果数组。

目前您创建了一个新的arr,它应该存储每次调用problem1 的结果,这是错误的,因为这个新数组与之前创建的数组无关。

考虑在problem1 方法的调用之外创建它。您始终可以将其作为附加参数传递并“累积”结果(甚至有一个术语称为“累加器”,因此arr 将是一个累加器)。

还要考虑返回结果(提示:使用累加器方法,您实际上不需要返回任何内容)

既然是作业,显然我不会给出完整的解决方案,让你尽情享受;)

【讨论】:

  • 我应该提到我不能更改方法头。我只被允许传入一个节点。我将如何在 issue1 的调用之外创建它?
  • @Cyber​​knightMk1 只需创建另一个带有 2 个参数(根、数组)的函数,由问题 1 调用。
  • 在我原来的帖子中,我说我只能有一个功能来做到这一点。对不起,我会说得更清楚。
  • 我的教授不允许我们添加辅助函数。
  • 哦 - 我错过了这里所有的 cmets 派对;)@Roy Lee 给出的建议显然来自一位经验丰富的软件开发人员,这就是我们在“现实生活中如何解决它” ” :) 现在你真的应该问你的教授这点——可能他错过了这一点。您仍然可以选择在包含static int[] problem1(Node root) 定义的类中使用“静态字段”arr,但它会很丑陋。最重要的是,我们都指出了当前实现的问题,但我们无法更改分配:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多