通过链表使用java的二叉树数据结构

问题描述 投票:0回答:0
import java.util.Scanner;

public class Tree {

    static Node create() {
        int data;
        Scanner sc = new Scanner(System.in);
        Node root = null;
        System.out.println("Enter the value");
        data = sc.nextInt();
        if (data == -1)
            return null;
        root = new Node(data);
        System.out.println("Enter left child of " + root);
        root.left = create();
        System.out.println("Enter right child of" + root);
        root.right = create();
        return root;
    }

    public static void main(String[] args) {
        Node root = create();
    }
}

class Node {
    Node left, right;
    int data;

    public Node(int data) {
        this.data = data;
        this.left = null;
        this.right = null;
    }
}

我无法获得所需的输出。当我输入值时,它会再次询问该值而不是询问子部分。请帮助。

java linked-list tree binary-tree dsa
© www.soinside.com 2019 - 2024. All rights reserved.