/**
* Created by lxw, liwei4939@126.com on 2017/10/25.
*/
public class convert {
class Node{
public int value;
public Node left;
public Node right;
public Node(int data){
this.value = data;
}
}
public Node convert2(Node head){
if(head == null)
return null;
Node last = process(head);
head = last.right;
last.right = null;
return head;
}
public Node process(Node head){
if(head == null)
return null;
Node leftE = process(head.left);
Node rightE = process(head.rigth);
Node leftS = leftE != null? leftE.right:null;
Node rightS = rightE != null? rightE.right:null;
if(leftE != null && rightE != null){
leftE.right = head;
head.left = leftE;
rigthS.left = head;
head.right = rightS;
rightE.left = leftS;
return rightE;
} else if(leftE != null){
head.left = leftE;
leftE.right = head;
head.right = leftS;
return head;
} else if(rightE != null){
rightS.left = head;
head.right = rightS;
rightE.left = head;
return rightE;
} else{
head.right = head;
return head;
}
}
}
将搜索二叉树转换成双向链表
最新推荐文章于 2024-08-14 21:25:09 发布