public static BiNode linklist(BiNode root)
{
BiNode head = null, tail=null;
convertBST(head, tail, root);
return head;
}
public static void convertBST(BiNode head, BiNode tail, BiNode root)
{
BiNode leftTail = null, rightHead = null;
if(root==null){
head = null;
tail = null;
return;
}
System.out.println("root = "+root.key);
convertBST(head, leftTail, root.node1);
convertBST(rightHead, tail, root.node2);
if(leftTail != null)
{
System.out.println("leftTail = "+leftTail.key);
leftTail.node2 = root;
root.node1 = leftTail;
}else{
head = root;
System.out.println("head = "+ head.key+", root = "+root.key);
}
if(rightHead != null)
{
rightHead.node1 = root;
root.node2 = rightHead;
}else{
tail = root;
System.out.println("tail = "+ tail.key+", root = "+root.key);
}
}
以上是我用于将 BST 转换为双链接列表的 java 代码。
但是不知道为什么head总是变,应该是指向链表的head而不变。
我很高兴伟大的头脑会帮助我调试这段代码!谢谢!!!