题目描述
输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的指向。
package xidian.lili.niuke;
public class ConvertTreeNode {
TreeNode lefthead=null;
TreeNode righthead=null;
public TreeNode Convert(TreeNode pRootOfTree) {
if(pRootOfTree==null){
return null;
}
Convert(pRootOfTree.left);
if(righthead==null){
righthead=lefthead=pRootOfTree;
}else{
righthead.right=pRootOfTree;
pRootOfTree.left=righthead;
righthead=pRootOfTree;
}
Convert(pRootOfTree.right);
return lefthead;
}
}
class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
根据二叉搜索树,左子树小于根节点,右子树大于根节点
要得到排序的双向链表,按照中序遍历的顺序将节点串联起来