第一题 二叉搜索树与双向链表
https://www.cnblogs.com/edisonchou/p/4793345.html
解题思路待补充
package com.lyc.dataautest;
public class Convert {
public static BinaryTreeNode Convert(BinaryTreeNode root) {
BinaryTreeNode last = null;
convertnode(root,last);
//找到头结点并返回
BinaryTreeNode headInList = last;
while (headInList != null && headInList.left != null)
{
headInList = headInList.left;
}
return headInList;
}
public static void convertnode(BinaryTreeNode root,BinaryTreeNode lastnode) {
if(root==null) return;
BinaryTreeNode current = root;
//左子树的递归
if(current.left!=null) {
convertnode(current.left,lastnode);
}
//左子树建立双向链表
current.left=lastnode;
if(lastnode!=null) {
lastnode.right=current;
}
//指针迁移,并递归右子树
lastnode = current;
if(current.right!=null) {
convertnode(current.right,lastnode);
}
}
}
网友评论