英文:
Returning the number of nodes within a range in a binary tree
问题
我正在尝试编写一个方法,该方法返回二叉树中数值在某个范围内的节点数量。
以下是您请求的完整代码的翻译部分:
public class StaffInfo {
final String name;
final int monthHired;
public StaffInfo(String name, int monthHired){
this.name = name;
this.monthHired = monthHired;
}
}
public class StaffTree implements Iterable<StaffInfo>{
public StaffNode root;
public StaffTree(StaffInfo c) {
this.root = new StaffNode(c);
}
private StaffTree(StaffNode c) {
this.root = c;
}
}
class StaffNode {
StaffInfo data;
StaffNode senior;
StaffNode same;
StaffNode junior;
public StaffNode(StaffInfo data) {
this.data = data;
this.senior = null;
this.same = null;
this.junior = null;
}
}
public int numInRange(int monthMin, int monthMax) {
int count = 0;
if (monthMin > monthMax) {
return 0;
}
if (root.data.monthHired >= monthMin && root.data.monthHired <= monthMax) {
count++;
}
if (root.senior != null) {
root.senior.numInRange(monthMin, monthMax);
}
if (root.same != null) {
root.same.numInRange(monthMin, monthMax);
}
if (root.junior != null) {
root.junior.numInRange(monthMin, monthMax);
}
return count;
}
请注意,您提供的代码中可能存在问题,可能会导致StackOverflowError
。如果您需要进一步的帮助,请提供更多信息。
英文:
I am trying to code a method that returns the number of nodes in a binary tree who have a value between a range.
Here if the full code as requested:
public class StaffInfo {
final String name;
final int monthHired;
public StaffInfo(String name, int monthHired){
this.name = name;
this.monthHired = monthHired;
}
public class StaffTree implements Iterable<StaffInfo>{
public StaffNode root;
public StaffTree(StaffInfo c) {
this.root = new StaffInfo(c);
}
private StaffTree(StaffNode c) {
this.root = c;
}
class StaffNode {
StaffInfo data;
StaffNode senior;
StaffNode same;
StaffNode junior;
public StaffNode(StaffInfo data) {
this.data = data;
this.senior = null;
this.same = null;
this.junior = null;
}
Here is the code for the method I have trouble with:
public int numInRange(int monthMin, int monthMax) {
int count = 0;
if (monthMin > monthMax) {
return 0;
}
if (root.data.monthHired >= monthMin && root.data.monthHired <= monthMax) {
count++;
}
if (root.senior != null) {
root.senior.numInRange(monthMin, monthMax);
}
if (root.same != null) {
root.same.numInRange(monthMin, monthMax);
}
if (root.junior != null) {
root.junior.numInRange(monthMin, monthMax);
}
return count;
I am mimicking an office thus each node can have a child that's a senior, junior, or same (determined by the hiring date). monthMin and monthMax are both integers representing the number of months since January 2015.
When I run the code above, I get a StackOverFlowError.
Any help is appreciated!
If the problem is unclear, please let me know in the comments and I will edit it right away.
答案1
得分: 1
你使用了全局变量 root,所以每当 root 调用它的子节点时,会无限次发生。您需要在函数中将子节点作为 root 传递。然后您可以进行计数。
public int numInRange(Root root, int monthMin, int monthMax) {
int count = 0;
if (monthMin > monthMax) {
return 0;
}
if (root.data.monthHired >= monthMin && root.data.monthHired <= monthMax) {
count++;
}
if (root.senior != null) {
count += numInRange(root.senior, monthMin, monthMax);
}
if (root.same != null) {
count += numInRange(root.same, monthMin, monthMax);
}
if (root.junior != null) {
count += numInRange(root.junior, monthMin, monthMax);
}
return count;
}
尝试使用这个版本。
英文:
You used root as global variable thats why every time root call his child. it will be happens infinite time. You need to pass child as root in function. then u can count.
public int numInRange(Root root, int monthMin, int monthMax) {
int count = 0;
if (monthMin > monthMax) {
return 0;
}
if (root.data.monthHired >= monthMin && root.data.monthHired <= monthMax) {
count++;
}
if (root.senior != null) {
root.senior.numInRange(root.senior,monthMin, monthMax);
}
if (root.same != null) {
root.same.numInRange(root.same,monthMin, monthMax);
}
if (root.junior != null) {
root.junior.numInRange(root.junior,monthMin, monthMax);
}
return count;
}
Try with this.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论