【剑指Offer学习】【面试题19 :二叉树的镜像】
来源:互联网 发布:淘宝店铺如何设置满减 编辑:程序博客网 时间:2024/05/17 07:31
题目:请完成一个函数,输入一个二叉树,该函数输出它的镜像。
二叉树结点的定义:
/** * 二叉树的树结点 */public static class BinaryTreeNode { int value; BinaryTreeNode left; BinaryTreeNode right;}
解题思路:
我们先前序遍历这棵树的每个结点,如果遍历到的结点有子结点,就交换它的两个子结点。当交换完所有非叶子结点的左右子结点之后,就得到了树的镜像。
代码实现:
public class Test19 { /** * 二叉树的树结点 */ public static class BinaryTreeNode { int value; BinaryTreeNode left; BinaryTreeNode right; } /** * 请完成一个函数,输入…个二叉树,该函数输出它的镜像 * * @param node 二叉树的根结点 */ public static void mirror(BinaryTreeNode node) { // 如果当前结点不为空则进行操作 if (node != null) { // 下面是交换结点左右两个子树 BinaryTreeNode tmp = node.left; node.left = node.right; node.right = tmp; // 对结点的左右两个子树进行处理 mirror(node.left); mirror(node.right); } } public static void printTree(BinaryTreeNode node) { if (node != null) { printTree(node.left); System.out.print(node.value + " "); printTree(node.right); } } public static void main(String[] args) { // 8 // / \ // 6 10 // / \ / \ // 5 7 9 11 BinaryTreeNode root = new BinaryTreeNode(); root.value = 8; root.left = new BinaryTreeNode(); root.left.value = 6; root.left.left = new BinaryTreeNode(); root.left.left.value = 5; root.left.right = new BinaryTreeNode(); root.left.right.value = 7; root.right = new BinaryTreeNode(); root.right.value = 10; root.right.left = new BinaryTreeNode(); root.right.left.value = 9; root.right.right = new BinaryTreeNode(); root.right.right.value = 11; printTree(root); System.out.println(); mirror(root); printTree(root); // 1 // / // 3 // / // 5 // / // 7 // / // 9 BinaryTreeNode root2 = new BinaryTreeNode(); root2.value = 1; root2.left = new BinaryTreeNode(); root2.left.value = 3; root2.left.left = new BinaryTreeNode(); root2.left.left.value = 5; root2.left.left.left = new BinaryTreeNode(); root2.left.left.left.value = 7; root2.left.left.left.left = new BinaryTreeNode(); root2.left.left.left.left.value = 9; System.out.println("\n"); printTree(root2); System.out.println(); mirror(root2); printTree(root2); // 0 // \ // 2 // \ // 4 // \ // 6 // \ // 8 BinaryTreeNode root3 = new BinaryTreeNode(); root3.value = 0; root3.right = new BinaryTreeNode(); root3.right.value = 2; root3.right.right = new BinaryTreeNode(); root3.right.right.value = 4; root3.right.right.right = new BinaryTreeNode(); root3.right.right.right.value = 6; root3.right.right.right.right = new BinaryTreeNode(); root3.right.right.right.right.value = 8; System.out.println("\n"); printTree(root3); System.out.println(); mirror(root3); printTree(root3); }}
运行结果:
0 0
- [剑指offer][面试题19]二叉树的镜像
- 【剑指offer】面试题19:二叉树的镜像
- 剑指offer 面试题19 二叉树的镜像
- 剑指Offer:面试题19 二叉树的镜像
- 《剑指Offer》面试题19:二叉树的镜像
- 剑指offer-面试题19:二叉树的镜像
- 剑指Offer----面试题19:二叉树的镜像
- 剑指offer面试题19:二叉树的镜像
- 剑指offer面试题19:二叉树的镜像
- 剑指offer面试题19:二叉树的镜像
- 剑指offer--面试题19:二叉树的镜像
- 剑指offer-面试题19-二叉树的镜像
- 【剑指offer】面试题19:二叉树的镜像
- 【面试题】剑指Offer-19-二叉树的镜像
- 剑指offer-面试题19-二叉树的镜像
- 剑指offer-面试题 19:二叉树的镜像
- 剑指offer 面试题19 二叉树的镜像
- 【剑指offer】面试题19:二叉树的镜像
- 【剑指Offer学习】【面试题18 :树的子结构】
- MongoDB与MySQL的操作对比表及区别介绍
- 雷军武大演讲:快40岁时 我曾觉得梦想渐行渐远
- [Android5 系列—] 1. 构建一个简单的用户界面
- HBase客户端API基本操作
- 【剑指Offer学习】【面试题19 :二叉树的镜像】
- 《统计学习方法》笔记(五)--朴素贝叶斯
- C++和C的区别
- 【笔记】Linux驱动学习第三章
- Ruby高手点评Scala编程语言十大绝招
- [华为机试练习题]5.IP地址判断有效性
- 多线程相关
- 洛谷1017 进制转换
- http://www.blogchen.com/archives/category/转载/page/2