題目:輸入一顆二叉搜索樹,將該二叉搜索樹轉換成一個排序的雙向鏈表。要求不能創建新的結點,只能調整樹中結點指針的指向。
比如如下圖中的二叉搜索樹,則輸出轉換之後的排序雙向鏈表爲:
由於要求轉換之後的鏈表是排好序的,我們可以中序遍歷樹的每一個結點,這是因爲中序遍歷算法的特點是按照從小到大的順序遍歷二叉樹的每一個結點。當遍歷到根結點的時候,我們把樹看成三部分:值爲10的結點、根結點爲6的左子樹、根結點爲14的右子樹。根據排序鏈表的定義,值爲10的結點將和它左子樹最大一個結點鏈接起來,同時它還將和右子樹的最小的結點鏈接起來。按照中序遍歷的順序,當我們遍歷轉換到根結點時,它的左子樹已經轉換成一個排序的鏈表了,並且處在鏈表中的最後一個結點是當前值最大的結點。接着我們去遍歷轉換右子樹,並把根結點和右子樹中最小的結點鏈接起來。左、右子樹的轉換方法相同,很自然地想到可以用遞歸去做。
public BinaryTreeNode convert(BinaryTreeNode root){
BinaryTreeNode node = null;
convert(root,node);
while(node != null && node.leftNode != null){
node = node.leftNode;
}
return node;
}
public void convert(BinaryTreeNode root,BinaryTreeNode lastNode){
if(root == null)
return;
BinaryTreeNode current = root;
if(current.leftNode != null)
convert(current.leftNode,lastNode);
current.leftNode = lastNode;
if(lastNode != null)
lastNode.rightNode = current;
if(current.rightNode != null)
convert(current.rightNode,lastNode);
}