二叉搜索树与双向链表Java
2016-03-08 18:51
645 查看
题目:输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的方向。
思路:在二叉搜索树中,左子结点的值总是小于父子结点的值,右子结点的值总是大于父结点的值。因此在转换成排序的双向链表时,原先指向左子节点的值调整为 链表中指向前一个结点的指针,原先指向右子结点的指针调整为链表中指向后一个结点的指针。
根结点,左子树和右子树。在把左,右子树都转换成排序的双向链表之后再和根结点连接起来,整棵二叉搜索树也就转换成了排序的双向链表。
package offer;
/**
*二叉搜索树与双向链表
*输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何结点,只能调整树中结点指针的指向。
*/
public class Convert {
public static void main(String[] args) {
int[] pre={10,6,4,8,14,12,16};
int[] inOrder = {4,6,8,10,12,14,16};
BinaryTreeNode root = RebuildBinaryTree.rebuildBinaryTree(pre, inOrder);
root.preOrder(root);
BinaryTreeNode head = convert(root);
BinaryTreeNode tail = null;
System.out.println(head==null);
while(head!=null){
System.out.print(head.val+" ");
tail = head;
head = head.right;
}
System.out.println();
while(tail!=null){
System.out.print(tail.val+" ");
tail = tail.left;
}
}
public static BinaryTreeNode convert(BinaryTreeNode root){
BinaryTreeNode lastNodeInList = null;
lastNodeInList = convertNode(root,lastNodeInList);
BinaryTreeNode headOfList = lastNodeInList;
while(headOfList!=null && headOfList.left!=null){
headOfList = headOfList.left;
}
return headOfList;
}
public static BinaryTreeNode convertNode(BinaryTreeNode node, BinaryTreeNode lastNodeInList){
if(node==null)
return null;
BinaryTreeNode current = node;
if(current.left!=null)
lastNodeInList = convertNode(current.left,lastNodeInList);
current.left = lastNodeInList;
if(lastNodeInList!=null)
lastNodeInList.right = current;
lastNodeInList = current;
if(current.right!=null)
lastNodeInList = convertNode(current.right,lastNodeInList);
return lastNodeInList;
}
}
思路:在二叉搜索树中,左子结点的值总是小于父子结点的值,右子结点的值总是大于父结点的值。因此在转换成排序的双向链表时,原先指向左子节点的值调整为 链表中指向前一个结点的指针,原先指向右子结点的指针调整为链表中指向后一个结点的指针。
根结点,左子树和右子树。在把左,右子树都转换成排序的双向链表之后再和根结点连接起来,整棵二叉搜索树也就转换成了排序的双向链表。
package offer;
/**
*二叉搜索树与双向链表
*输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何结点,只能调整树中结点指针的指向。
*/
public class Convert {
public static void main(String[] args) {
int[] pre={10,6,4,8,14,12,16};
int[] inOrder = {4,6,8,10,12,14,16};
BinaryTreeNode root = RebuildBinaryTree.rebuildBinaryTree(pre, inOrder);
root.preOrder(root);
BinaryTreeNode head = convert(root);
BinaryTreeNode tail = null;
System.out.println(head==null);
while(head!=null){
System.out.print(head.val+" ");
tail = head;
head = head.right;
}
System.out.println();
while(tail!=null){
System.out.print(tail.val+" ");
tail = tail.left;
}
}
public static BinaryTreeNode convert(BinaryTreeNode root){
BinaryTreeNode lastNodeInList = null;
lastNodeInList = convertNode(root,lastNodeInList);
BinaryTreeNode headOfList = lastNodeInList;
while(headOfList!=null && headOfList.left!=null){
headOfList = headOfList.left;
}
return headOfList;
}
public static BinaryTreeNode convertNode(BinaryTreeNode node, BinaryTreeNode lastNodeInList){
if(node==null)
return null;
BinaryTreeNode current = node;
if(current.left!=null)
lastNodeInList = convertNode(current.left,lastNodeInList);
current.left = lastNodeInList;
if(lastNodeInList!=null)
lastNodeInList.right = current;
lastNodeInList = current;
if(current.right!=null)
lastNodeInList = convertNode(current.right,lastNodeInList);
return lastNodeInList;
}
}
相关文章推荐
- Java并发概述
- java cookie
- 泛型继承接口Demo
- 做一个合格的程序猿之浅析Spring IoC源码(一)分析前的准备和思考
- JAVA学习计划
- 从海量数据中查找出前k个最小或最大值的算法(java)
- JAVA环境变量配置
- java static 放在哪
- spring+springMvc+mybatis 记录备忘
- eclipse vi风格
- Java I/O学习(附实例和详解)
- Java I/O学习(附实例和详解)
- spring中scope作用域(转)
- 使用spring-data-solr做solr客户端
- Java单例模式七种实现方式
- Java Web并发访问的线程安全问题
- java IO之字节流和字符流比较
- java解析字符串拆分单独元素
- 1.单例模式
- spring的自动装配Bean与自动检测Bean