/**
* 二叉树转换为双向链表。
*
* 中序遍历
*
*
*/
public class LinkList {
public Node head;
public Node pre;
public void inOrder(Node node) {
if(node.left != null){
inOrder(node.left);
}
if(head == null){
head = node;
}else{
pre.right = node;
node.left = pre;
}
pre = node;
if(node.right != null){
inOrder(node.right);
}
}
public Node getListFromTree(Node root){
assert(true);
inOrder(root);
return head;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
LinkList ll = new LinkList();
BinaryTree bt = new BinaryTree();
int[] a = {10,1,2,3,4,5,6,7,8,9};
Node root = bt.buildTree(a);
bt.printTree();
ll.inOrder(root);
Node tail = null;
Node head = ll.head;
while (head != null) {
System.out.print(head.value + " ");
head = head.right;
if (head != null && head.right == null) {
tail = head;
}
}
System.out.println(" ");
while (tail != null) {
System.out.print(tail.value + " ");
tail = tail.left;
}
System.out.println(" ");
}
}
二叉树转换为双向链表
最新推荐文章于 2019-08-12 16:06:18 发布