【问题标题】:Returning the number of nodes within a range in a binary tree返回二叉树范围内的节点数
【发布时间】:2020-07-18 06:03:36
【问题描述】:

我正在尝试编写一种方法,该方法返回二叉树中具有一个范围内的值的节点数。

这里如果要求的完整代码:

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;
        }

这是我遇到问题的方法的代码:

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;

我正在模仿一个办公室,因此每个节点都可以有一个大四、大三或相同的孩子(由招聘日期决定)。 monthMin 和 monthMax 都是整数,表示自 2015 年 1 月以来的月数。

当我运行上面的代码时,我得到一个 StackOverFlowError。

感谢任何帮助!

如果问题不清楚,请在cmets中告诉我,我会立即修改。

【问题讨论】:

  • 你的hiredFromMonths方法在哪里
  • 一个错字,刚刚改正。
  • 你需要在 numInRange 方法中传递 root 否则相同的方法调用无限次
  • 有没有办法做到这一点,因为该方法是在 root 上调用的,所以不将 root 作为参数?
  • 是的。但它更好地通过。我回答了。检查

标签: java recursion count binary-tree binary-search-tree


【解决方案1】:

您使用 root 作为全局变量,这就是为什么每次 root 都调用他的孩子。它会发生无限的时间。您需要在函数中将 child 作为 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) {
                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;
}

试试这个。

【讨论】:

  • 有没有办法在不将 root 作为参数传递的情况下修改代码。为澄清起见,root 表示“this”,因为该方法是在 root 上调用的。
  • 你的方法在 StaffTree 类中吗?
  • 不,它在 StaffNode 下
  • 我在 StaffTree 类中看到了根目录
  • CatNode 是 CatTree 下的一个类
猜你喜欢
  • 1970-01-01
  • 2023-04-07
  • 1970-01-01
  • 1970-01-01
  • 2020-02-07
  • 1970-01-01
  • 2020-02-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多