镜像二叉树

package test;

import java.util.LinkedList;


public class Test3 {
	static class Node{
		int value = -1;
		Node lnext = null;
		Node rnext = null;
		public Node(int i){
			value = i;
		}
		
		@Override
		public String toString() {
			return value+"";
		}
	}		
	
	static void mirror(Node r1){
		if(r1.lnext==null && r1.rnext==null) return;
		if(r1.lnext!=null) mirror(r1.lnext);
		if(r1.rnext!=null) mirror(r1.rnext);
		Node n = r1.lnext;
		r1.lnext = r1.rnext;
		r1.rnext = n;
	}
	
	static void print_tree(Node r,LinkedList<Node> arr){
		arr.add(r);
		while(arr.size()!=0){
			Node n = arr.pop();
			System.out.println(n);
			if(n.lnext!=null) arr.add(n.lnext);
			if(n.rnext!=null) arr.add(n.rnext);
		}	
	}
	
	public static void main(String[] args) {		
		Node root1 = new Node(8);
		Node n1 = new Node(6);
		Node n2 = new Node(10);
		root1.lnext = n1;
		root1.rnext = n2;
		Node n3 = new Node(5);
		Node n4 = new Node(7);
		n1.lnext = n3;
		n1.rnext = n4;
		Node n5 = new Node(9);		
		Node n6 = new Node(11);
		n2.lnext = n5;
		n2.rnext = n6;
		
		LinkedList<Node> arr = new LinkedList<Node>();
		mirror(root1);
		
		print_tree(root1, arr);		
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值