【发布时间】:2013-07-02 16:48:53
【问题描述】:
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);
}
}
上面是我的java代码,用于将BST转换为双链表。
但是不知道为什么head总是变,应该是指向链表的head而不变。
我很高兴伟大的头脑能帮助我调试这段代码!谢谢!!!
【问题讨论】:
-
你能把
linklist()和convertBST()的代码贴出来吗?其他代码似乎都没有对链表逻辑的 BST 做出贡献,所以它只会把所有东西都弄乱了。如果所有内容都整齐地缩进,也会有所帮助。
标签: java algorithm linked-list binary-search-tree