微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么我不能使这些函数成为静态的?

如何解决为什么我不能使这些函数成为静态的?

为什么我不能只制作 inorder 并插入静态并在主函数中传递对象的根? 而不是创建具有 void 返回类型的相同 naem 的临时函数,然后在主函数调用它们

public TreeNode insert(TreeNode root,int value){ - make this static
        if (root == null){
            root = new TreeNode(value);
            return root;
        }
        if (value<root.data){
            root.left = insert(root.left,value);
        }else {
            root.right = insert(root.right,value);
        }
        return root;
    }
    public void insert(int value){ i am asking if i can eleminate this function
        root = insert(root,value);
    }
    public void inorder(){ - and this function 
        inorder(root);
    }
    public void inorder(TreeNode temp){ - make this static
        if (temp == null){
            return;
        }
        inorder(temp.left);
        System.out.print(temp.data);
        inorder(temp.right);
    }

在主函数中,我将直接调用 insert(object.root,) 和 inorder(object.root) 我这样做了,但没有显示任何输出

解决方法

你可以,但是你应该调用 object.root = insert(object.root,...) 函数 insert(int value) 修改对象根

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。