求二叉树镜像(破坏二叉树和不破坏二叉树使用非递归实现求解二叉树镜像)

求二叉树镜像(破坏二叉树和不破坏二叉树使用非递归实现求解二叉树镜像)

实现过程如下所示:

package cn.edu.nwu.tree;

import java.util.Stack;

/**
 * @author jcm
 *
 *时间 2016年9月16日
 */
public class GetTreeNodeMirror {
	public static void main(String[] args) {
		TreeNode root = CreateBinaryTree.createTreeNode();
		System.out.println("交换前的二叉树");
		GetTreeNodeInOrderTraver.inOrderRecursion(root);
		System.out.println();
		System.out.println("递归实现二叉树中序遍历");
		GetTreeNodeInOrderTraver.inOrder(getTreeNodeCopyMirror(root));
		System.out.println();
		System.out.println("非递归实现二叉树中序遍历");
		getTreeNodeMirror(root);
		GetTreeNodeInOrderTraver.inOrderRecursion(root);
	}
	/**
	 * 求二叉树的镜像
	 * 这个方法可以看出,他已经破坏了原来的二叉树
	 * (1)如果二叉树为空,直接return
	 * (2)如果二叉树不为空,求左子树和右子树的镜像
	 * (3)交换左子树和右子树 
	 * (4)原来的树根直接指向交换后的左子树和右子树
	 * @param root
	 * @return
	 */
	private static void getTreeNodeMirror(TreeNode root){
		if(root == null){  
            return;  
        }  
        Stack<TreeNode> stack = new Stack<TreeNode>();  
        stack.push(root);  
        while( !stack.isEmpty() ){  
            TreeNode current = stack.pop();  
            // 交换左右孩子  
            TreeNode temp = current.rightRight;  
            current.rightRight = current.leftChild;  
            current.leftChild = temp;  
            if(current.rightRight != null){  
                stack.push(current.rightRight);  
            }  
            if(current.leftChild != null){  
                stack.push(current.leftChild);  
            }  
        }
	}
	/**
	 * 求二叉树的镜像
	 * 这个方法可以看出,如果不能破坏二叉树,则需要定义一个新节点,分别指向二叉树的左子树和右子树
	 * (1)如果二叉树为空,返回空 
	 * (2)如果二叉树不为空,求左子树和右子树的镜像,
	 * (3)交换左子树和右子树 
	 * @param root
	 * @return
	 */
	private static TreeNode getTreeNodeCopyMirror(TreeNode root){
		if(root == null){  
            return null;  
        }  
        Stack<TreeNode> stack = new Stack<TreeNode>();  
        Stack<TreeNode> newStack = new Stack<TreeNode>();  
        stack.push(root);  
        TreeNode newRoot = new TreeNode(root.data);  
        newStack.push(newRoot);  
        while(!stack.isEmpty()){  
            TreeNode current = stack.pop();  
            TreeNode newCur = newStack.pop();  
            if(current.rightRight != null){  
                stack.push(current.rightRight);  
                newCur.leftChild = new TreeNode(current.rightRight.data);  
                newStack.push(newCur.leftChild);  
            }  
            if(current.leftChild != null){  
                stack.push(current.leftChild);  
                newCur.rightRight = new TreeNode(current.leftChild.data);  
                newStack.push(newCur.rightRight);  
            }  
        }  
        return newRoot;  
	}
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值