数据结构无法为二叉树、Java数据结构中的无序遍历获得正确的结果
我已经在二叉树数据结构中编写了预序和索引遍历的代码,但是预序遍历的结果是正确的,但是索引遍历中出现了一些错误,有人能告诉我代码中的错误吗。 提前谢谢
public class treepractice {
static Node root = null;
static class Node{
int data;
Node left, right;
Node(int d){
data = d;
left=right = null;
}
}
public static void main(String[] agrs){
treepractice tree = new treepractice();
tree.root = new Node(1);
tree.root.left = new Node(2);
tree.root.right = new Node(3);
tree.root.left.left = new Node(4);
tree.root.left.right = new Node(5);
// root.right.left = new Node(6);
tree.printInorder(root);
System.out.println();
tree.printPreorder(root);
System.out.println();
}
private static void printPreorder(Node root) {
if(root == null)
return;
System.out.print(root.data + " ");
printPreorder(root.left);
printPreorder(root.right);
}
private static void printInorder(Node root) {
if(root == null)
return;
printPreorder(root.left);
System.out.print(root.data + " ");
printPreorder(root.right);
}
}
共 (0) 个答案