Java 如何将二叉搜索树的节点值存储在字符串中?

Java 如何将二叉搜索树的节点值存储在字符串中?,java,binary-tree,Java,Binary Tree,我试图以字符串的升序将节点的所有值存储在二叉搜索树中。我设法把它们打印出来,但把它们存储在一个字符串中似乎有点复杂,有什么想法吗 我是这样打印出来的: public void inOrder() { if (this.left != null) { this.left.inOrder(); } System.out.print(this.value + " "); if (this.right != null) { this.rig

我试图以字符串的升序将节点的所有值存储在二叉搜索树中。我设法把它们打印出来,但把它们存储在一个字符串中似乎有点复杂,有什么想法吗

我是这样打印出来的:

public void inOrder() {
    if (this.left != null) {
        this.left.inOrder();
    }
    System.out.print(this.value + " ");
    if (this.right != null) {
        this.right.inOrder();
    }
}
我正在寻找这种方法:

public String inOrder() {
    // ???
}

由于递归调用搜索函数,我建议通过


不要试图从函数调用本身返回字符串。

好的,因为这里没有完整的代码示例,我将自己发布它。然而,我想知道为什么互联网上没有一个例子。我们开始吧。我假设二叉搜索树使用整数作为其节点的值:

public String inOrder() {
    String asc = "";
    if (this.left != null) {
        asc += this.left.inOrder();
    }

    asc = asc + this.value + " ";

    if (this.right != null) {
        asc += this.right.inOrder();
    }
    return asc;
}
如果你觉得这有帮助或者你有更好的方法,请留下评论

public String inOrder() {
    String asc = "";
    if (this.left != null) {
        asc += this.left.inOrder();
    }

    asc = asc + this.value + " ";

    if (this.right != null) {
        asc += this.right.inOrder();
    }
    return asc;
}