题目描述
输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的指向。
import java.util.LinkedList;
import java.util.Queue;
public class Solution {
Queue<TreeNode> queue = new LinkedList<TreeNode>();
public TreeNode Convert(TreeNode pRootOfTree) {
if(pRootOfTree == null) {
return null;
}
inOrder(pRootOfTree);
TreeNode root = queue.poll();
root.left = null;
TreeNode pre = root;
while(!queue.isEmpty()) {
TreeNode temp = queue.poll();
pre.right = temp;
temp.left = pre;
pre = temp;
}
return root;
}
private void inOrder(TreeNode node) {
if(node != null) {
inOrder(node.left);
queue.add(node);
inOrder(node.right);
}
}
}