无法获得二叉树、Java数据结构中的顺序遍历的正确结果
我已经在二叉树数据结构中编写了预序和索引遍历的代码,但是预序遍历的结果是正确的,但是索引遍历中出现了一些错误,有人能告诉我代码中的错误吗。 提前谢谢无法获得二叉树、Java数据结构中的顺序遍历的正确结果,java,data-structures,binary-tree,Java,Data Structures,Binary Tree,我已经在二叉树数据结构中编写了预序和索引遍历的代码,但是预序遍历的结果是正确的,但是索引遍历中出现了一些错误,有人能告诉我代码中的错误吗。 提前谢谢 public class treepractice { static Node root = null; static class Node{ int data; Node left, right; Node(int d){ data = d; left=right = null;
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);
}
}
如果要从
printInorder
调用printInorder
方法,则必须调用printInorder
private static void printInorder(Node root) {
if (root == null)
return;
printInorder(root.left);
System.out.print(root.data + " ");
printInorder(root.right);
}
在您的代码中,printInorder方法应该递归调用相同的方法
private static void printInorder(Node root) {
if (root == null)
return;
printInorder(root.left);
System.out.print(root.data + " ");
printInorder(root.right);
}
请求调试帮助不适用于堆栈溢出问题。如果您对代码的特定功能有特定问题,请使用这些详细信息编辑您的问题。此外,无论你在哪里张贴这样的问题,不要只说“一些错误”。始终提供准确的错误代码、错误文本或问题行为的详细描述。