leetcode:二叉树遍历(morris)

package com.lz.second;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.lz.first.ReverseList.ListNode;

public class BinaryTree2 {
	public static class TreeNode{
		int val;
		TreeNode left;
		TreeNode right;
		int deep;
		TreeNode(int val,TreeNode left,TreeNode right){
			this.val=val;
			this.left=left;
			this.right=right;
		}
	}
	
	private static List<Integer> list=new ArrayList<>();
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		TreeNode node7 = new TreeNode(7,null,null); 
		TreeNode node6 = new TreeNode(6,null,null); 
		TreeNode node5 = new TreeNode(5,node6,node7); 
		TreeNode node4 = new TreeNode(4,null,null); 
		TreeNode node3 = new TreeNode(3,null,null); 
		TreeNode node2 = new TreeNode(2,node4,node5); 
		TreeNode node1 = new TreeNode(1,node2,node3); 
		//morrisPre(node1);
		//morrisIn(node1);
		morrisPost(node1);
		
	}
/**
           1
        2     3
      4   5      
         6 7  
 */
	//前序
	public static void morrisPre(TreeNode cur) {
		if(cur==null) {
			return;
		}
		TreeNode mostRight=null;
		while(cur!=null) {
			mostRight=cur.left;
			if(mostRight!=null) {
				while(mostRight.right!=null&&mostRight.right!=cur) {
					mostRight=mostRight.right;
				}
				if(mostRight.right==null) {//建立线索指针
					mostRight.right=cur;
					System.out.println(cur.val);
					cur=cur.left;
					continue;
				}else {
					mostRight.right=null;
				}
			}else {
				System.out.println(cur.val);
			}
			cur=cur.right;
		}	
	}
	
	
	//中序
	public static void morrisIn(TreeNode cur) {
		if(cur==null) {
			return;
		}
		TreeNode mostRight=null;
		while(cur!=null) {
			mostRight=cur.left;
			if(mostRight!=null) {
				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;
				}
			}
			System.out.println(cur.val);
			cur=cur.right;
		}	
	}
	
	
	//后序
	public static void morrisPost(TreeNode cur) {
		if(cur==null) {
			return;
		}
		TreeNode root=cur;
		TreeNode mostRight=null;
		while(cur!=null) {
			mostRight=cur.left;
			if(mostRight!=null) {
				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;
					printNode(cur.left);
				}
			}
			cur=cur.right;
		}	
		printNode(root);
	}
	private static void printNode(TreeNode head) {
		TreeNode tail=reverse(head);
		while(tail!=null) {
			System.out.println(tail.val);
			tail=tail.right;
		}
		reverse(tail);
	}
	private static TreeNode reverse(TreeNode head) {//链表反转
		TreeNode prev=null,tmp;
		TreeNode curr=head;
		while(curr!= null) {
			tmp=curr.right;
			curr.right=prev;
			prev=curr;
			curr=tmp;
		}
		return prev;
	}	
	
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值