输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。
要求不能创建任何新的结点,只能调整树中结点指针的指向。
public class Solution {
TreeNode tail = null;
public TreeNode Convert(TreeNode root) {
if(root == null){
return null;
}
TreeNode left = Convert(root.left);
if(left != null) {
tail.right = root;
root.left = tail;
}
tail = root;
TreeNode right = Convert(root.right);
if(right != null) {
root.right = right;
right.left = root;
}
return left != null ? left : root;
}
}