package common;
/**
* @author : zhaoliang
* @program :newCoder
* @description : 二叉搜索树与双向链表
* @create : 2020/11/28 16:24
*/
public class Convert {
//输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的指向。
public class TreeNode{
int val;
TreeNode left;
TreeNode right;
TreeNode(int val){
val = val;
}
}
private TreeNode pre=null;
private TreeNode head = null;
public TreeNode convert(TreeNode treeNode){
inOrder(treeNode);
return head;
}
private void inOrder(TreeNode root) {
if (root==null){
return;
}
inOrder(root.left);
root.left = pre;
if (pre!=null){
pre.right = root;
}
pre = root;
if (head==null){
head = root;
}
inOrder(root.right);
}
}
常见算法-二叉搜索树与双向链表
最新推荐文章于 2022-05-22 21:58:11 发布