博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
剑指Offer 二叉树的镜像
阅读量:4208 次
发布时间:2019-05-26

本文共 4394 字,大约阅读时间需要 14 分钟。

题目描述:

请完成一个函数,输入一个二叉树,该函数输出它的镜像。

思路:

遍历二叉树 如果当前节点存在左子树和右子树 那么将两个子树进行交换

这里写图片描述

1) 递归实现

package com.hqq.exercise.tree;/** * MirrorTree 二叉树的镜像 * 题目描述: * 请完成一个函数,输入一个二叉树,该函数输出它的镜像。 * 思路: * 遍历两个二叉树 如果当前节点存在子节点的话 就交换它的两个子节点 * Created by heqianqian on 2017/8/12. */public class MirrorTree {
/** * 输出一个二叉树的镜像[递归] * @param root 二叉树的根节点 */ public static void mirrorTreeByRecursion(BinaryTreeNode root){ if(root == null){ return; } if(root.leftNode==null && root.rightNode == null){ return; } //如果存在子节点 那么就交换它的两个子节点 BinaryTreeNode temp = root.leftNode; root.leftNode = root.rightNode; root.rightNode =temp; if (root.leftNode!=null){ mirrorTreeByRecursion(root.leftNode); } if (root.rightNode!=null){ mirrorTreeByRecursion(root.rightNode); } } /** * 输出一个二叉树的镜像[循环] * @param root 二叉树的根节点 */ public static void mirrorTreeByLoop(BinaryTreeNode root){ while (root!=null&&root.leftNode!=null&&root.rightNode!=null){ //交换节点 BinaryTreeNode temp = root.leftNode; root.leftNode = root.rightNode; root.rightNode = temp; } }}
package com.hqq.exercise.tree;import org.junit.Before;import org.junit.Test;import static org.junit.Assert.*;/** * MirrorTree 二叉树的镜像 * 题目描述: * 请完成一个函数,输入一个二叉树,该函数输出它的镜像。 * 思路: * 遍历两个二叉树 如果当前节点存在子节点的话 就交换它的两个子节点 * 样例输入: * ------8 * ---6     10 * --5 7   9  11 * {8,6,5,7,10,9,11} * 样例输出: * ---------8 * -----10     6 * --11   9   7   5 * {8,10,11,9,6,7,5} * Created by heqianqian on 2017/8/12. */public class MirrorTreeTest {
private BinaryTreeNode root; @Before public void setUp() throws Exception { root = new BinaryTreeNode(8); BinaryTreeNode leftChild = new BinaryTreeNode(10); leftChild.leftNode = new BinaryTreeNode(11); leftChild.rightNode = new BinaryTreeNode(9); BinaryTreeNode rightChild = new BinaryTreeNode(6); rightChild.leftNode = new BinaryTreeNode(7); rightChild.rightNode = new BinaryTreeNode(5); root.leftNode = leftChild; root.rightNode = rightChild; System.out.println("Before Mirror"); print(root); System.out.println(); } @Test public void test() throws Exception { MirrorTree.mirrorTreeByRecursion(root); System.out.println("After Mirror"); print(root); } /** * 先序遍历二叉树 */ private void print(BinaryTreeNode root) { System.out.print(root.value + " "); if (root.leftNode != null) { print(root.leftNode); } if (root.rightNode != null) { print(root.rightNode); } }}

运行结果:

Before Mirror8 10 11 9 6 7 5 After Mirror8 6 5 7 10 9 11

2) 非递归实现

1.思路:使用层序遍历+队列实现

层序遍历二叉树 如果当前节点的左右子树不为空 那么交换左右子树
如果左节点非空 左节点入队列
如果右节点非空 右节点入队列

public static void mirrorTreeByLoopLevelTraversal(BinaryTreeNode root) {        Queue
treeNodeQueue = new LinkedBlockingDeque<>(); while (root != null) { if (root.leftNode != null && root.rightNode != null) { BinaryTreeNode temp = root.leftNode; root.leftNode = root.rightNode; root.rightNode = temp; } if (root.leftNode != null) { treeNodeQueue.add(root.leftNode); } if (root.rightNode != null) { treeNodeQueue.add(root.rightNode); } if (!treeNodeQueue.isEmpty()) { root = treeNodeQueue.poll(); } else { break; } } }

2.使用先序遍历+栈

public static void mirrorTreeByLoopPreTraversal(BinaryTreeNode root) {        Stack
stack = new Stack<>(); stack.push(root); BinaryTreeNode temp = null; BinaryTreeNode t = null; while (!stack.isEmpty()) { temp = stack.pop(); //如果temp的左右子树不为空 那么交换左右子树 if (temp.leftNode != null && temp.rightNode != null) { t = temp.leftNode; temp.leftNode = temp.rightNode; temp.rightNode = t; } //如果左子树不为空 左子树入栈 if (temp.leftNode != null) { stack.push(temp.leftNode); } //如果右子树不为空 右子树入栈 if (temp.rightNode != null) { stack.push(temp.rightNode); } } }

你可能感兴趣的文章
安装.Net Framework 4.7.2时出现“不受信任提供程序信任的根证书中终止”的解决方法
查看>>
input type=“button“与input type=“submit“的区别
查看>>
解决Github代码下载慢问题!
查看>>
1.idea中Maven创建项目及2.对idea中生命周期的理解3.pom文件夹下groupId、artifactId含义
查看>>
LeetCode-栈|双指针-42. 接雨水
查看>>
stdin,stdout,stderr详解
查看>>
Linux文件和设备编程
查看>>
文件描述符
查看>>
终端驱动程序:几个简单例子
查看>>
登录linux密码验证很慢的解决办法
查看>>
fcntl函数总结
查看>>
HTML条件注释
查看>>
Putty远程服务器的SSH经验
查看>>
内核态与用户态
查看>>
使用mingw(fedora)移植virt-viewer
查看>>
趣链 BitXHub跨链平台 (4)跨链网关“初介绍”
查看>>
C++ 字符串string操作
查看>>
MySQL必知必会 -- 了解SQL和MySQL
查看>>
MySQL必知必会 -- 使用MySQL
查看>>
MySQL必知必会 -- 数据检索
查看>>