Java 二叉树:0、1或2个子节点的节点频率计数
我有一项任务: 我们为您指定了二叉树T的根节点。我们在T中区分了3种类型的节点:具有0个子节点的节点、具有1个子节点的节点和具有2个子节点的节点。为每种类型确定T中的节点数。将结果作为长度为3的整数数组返回 我得到了一个Java文件,它为这个算法生成随机测试用例 我只允许创建一个函数来完成所有这一切。我不允许将任何附加参数传递到下面的方法中。我也不允许在我创建的函数之外进行任何其他修改 在该文件中,已插入一个基本案例。我被告知使用递归按后序遍历树 我知道我的代码目前存在的问题。但我不知道如何修复它们 我目前的代码如下:Java 二叉树:0、1或2个子节点的节点频率计数,java,algorithm,binary-tree,Java,Algorithm,Binary Tree,我有一项任务: 我们为您指定了二叉树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;
}
节点类定义为:
static class Node {
public int value;
public Node left;
public Node right;
}
我认为您已经非常接近了,唯一需要解决的是处理结果数组 当前,您创建了一个新的
arr
,它应该存储problem1
的每次调用的结果,这是错误的,因为此新数组与以前创建的数组无关
考虑在调用problem1
方法之外创建它。您始终可以将其作为附加参数传递并“累加”结果(甚至还有一个术语称为“累加器”,因此arr
将是累加器)
还要考虑返回结果(提示:使用累加器方法,您实际上不需要返回任何内容)
因为这是一项任务,显然我不会给出一个完整的解决方案,留给你所有的乐趣;) 我应该提到我不能更改方法头。我只允许传入一个节点。如何在调用problem1之外创建它?@CyberknightMk1只需创建另一个带有2个参数(根、数组)的函数,由problem1调用即可。在我的原始帖子中,我声明我只能使用一个函数来实现这一点。对不起,我会说得更清楚一些。我的教授不允许我们添加助手函数。哦-我错过了这里所有的评论;)@Roy Lee给出的建议显然来自一位经验丰富的软件开发人员——这就是我们在“现实生活”中应该如何解决的问题:)现在你真的应该问你的教授这一点——可能他错过了这一点。在包含
static int[]problem1(节点根)
定义的类中,您仍然可以选择使用“static field”arr
,但这将很难看。总之,我们都指出了当前实现的问题,但我们不能更改分配:)我忘了提到我只允许将节点作为唯一的参数传递到函数中。函数必须返回整数数组。@CyberknightMk1那么您的教授是否允许任何全局变量?;)@CyberknightMk1好的,我已经根据您的约束更新了我的答案。这就成功了,我以前制作了多个数组,但我从未将它们设置为与递归调用相等。非常感谢。首先,是否允许您返回在方法所属的类中声明的静态变量?您的方法是否严格地只应返回本地数组?您是否介意分享节点
类的定义?我们需要更多的背景@AmalK我已经在我的帖子中添加了节点类定义。只要我不修改我函数之外的任何内容,但它必须是一个int数组。当您的问题得到回答时,请不要在标题中添加“已解决”之类的内容。接受答案是表明你的问题已经解决的方式。
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;
}
}