morris遍历的优点在于节省空间,因为不需要递归。
public class MorrisTraversal {
public static void main(String[] args) {
Node head = new Node(4);
head.left = new Node(2);
head.right = new Node(6);
head.left.left = new Node(1);
head.left.right = new Node(3);
head.right.left = new Node(5);
head.right.right = new Node(7);
morris(head);
morris2(head);
}
public static class Node{
public int value;
Node left;
Node right;
public Node(int data){
this.value = data;
}
}
public static void morris(Node head){
if(head == null){
return ;
}
Node cur = head;
Node mostRight = null;
boolean isRighNull = true;
while(cur != null){
System.out.print(cur.value+" ");
if(cur.left == null){ // 如果cur没有左孩子,cur向右移动
cur = cur.right;
}else{
mostRight = cur.left;
isRighNull = true;
while (mostRight.right != null) {
if(mostRight.right == cur){
isRighNull = false;
break;
}
mostRight = mostRight.right;
}
if (isRighNull) {
mostRight.right = cur;
cur = cur.left;
}else{
mostRight.right = null;
cur = cur.right;
}
}
}
System.out.println();
}
public static void morris2(Node head){
if(head == null){
return ;
}
Node cur = head;
Node mostRight = null;
while(cur != null){
System.out.print(cur.value+" ");
mostRight = cur.left;
if(mostRight != null){// 如果cur没有左孩子,cur向右移动
while (mostRight.right != null && mostRight.right != cur){
mostRight = mostRight.right;
}
if(mostRight.right == null){
mostRight.right = cur;
cur = cur.left;
continue;
}else{
mostRight.right = null;
}
}
cur = cur.right;
}
System.out.println();
}
}