二叉树的镜像 Love The Way You Lie 2023-03-14 13:14 32阅读 0赞 ## 一、前言 ## 《剑指Offer》中题27 ## 二、题目 ## 请完成一个函数,输入一棵二叉树,该函数输出它的镜像。叉树节点的定义如下: ![20200517230344591.png][] ## 三、思路 ## 递归实现即可 ## 四、扩展 ## 测试用例中需要判断两颗二叉树是否互为镜像。有三种方法: 方法一,将其中一颗二叉树转为镜像,并与另外一棵树进行比较判断各节点值是否相等 方法二,互为镜像的两个二叉树,每层数据顺序相反,逐层判断即可,需要注意结点为空的情况。前面文章中《从上往下打印二叉树》给出了获得逐层获得树数据方法。 方法三:递归实现——树1左结点和树2右结点判断;树1右结点和树2左结点判断。 详细介绍见[《二叉树镜像判断》][Link 1] 《二叉树镜像判断》 [https://blog.csdn.net/nie2314550441/article/details/106189949][Link 1] ## 五、编码实现 ## //========================================================================== /** * @file : MirrorOfBinaryTree.h * @purpose : 输入一棵二叉树,该函数输出它的镜像。 * */ //========================================================================== #pragma once #include "Common.h" #include "ConstructTree.h" #include "PrintTree.h" #include "GetTreesInLines.h" #include "MirrorOfBinaryTreeJudge.h" //输入两棵二叉树A和B,判断B是不是A的子结构。 #define NAMESPACE_MIRROROFBINARYTREE namespace NAME_MIRROROFBINARYTREE { #define NAMESPACE_MIRROROFBINARYTREEEND } template<class T> void MirrorOfBinaryTree(BinaryTreeNode<T>* pRoot) { if (pRoot == nullptr) { return; } if (pRoot->m_pLeft == nullptr && pRoot->m_pRight == nullptr) { return; } BinaryTreeNode<T>* pTemp = pRoot->m_pLeft; pRoot->m_pLeft = pRoot->m_pRight; pRoot->m_pRight = pTemp; if (pRoot->m_pLeft) { MirrorOfBinaryTree(pRoot->m_pLeft); } if (pRoot->m_pRight) { MirrorOfBinaryTree(pRoot->m_pRight); } } // // 测试 用例 START NAMESPACE_MIRROROFBINARYTREE template<class T> void test(const char* testName, BinaryTreeNode<T>* pRoot, BinaryTreeNode<T>* pRootMirror, T empty) { cout << "--------------- " << testName << " start ---------------" << endl; map<int, vector<T>> srcData; //记录的是每行节点值 GetTreesInLines(pRoot, srcData, empty); cout << "打印 原二叉树结构如下(" << "空节点用" << empty << "代替)" << endl; PrintTree((int)srcData.size(), srcData); map<int, vector<T>> mirrorData; //记录的是每行节点值 GetTreesInLines(pRootMirror, mirrorData, empty); cout << "打印 镜像二叉树结构如下(" << "空节点用" << empty << "代替)" << endl; PrintTree((int)mirrorData.size(), mirrorData); // 判断两个二叉树是否互为镜像 bool result_Solution1 = MirrorOfBinaryTreeJudge_Solution1(pRoot, pRootMirror); // 方法一 bool result_Solution2 = MirrorOfBinaryTreeJudge_Solution2(pRoot, pRootMirror); // 方法二 bool result_Solution3 = MirrorOfBinaryTreeJudge_Solution3(pRoot, pRootMirror); // 方法三 // 验证结果 if (result_Solution1) { cout << " Solution1,验证结果: passed." << endl; } else { cout << " Solution1,验证结果: failed." << endl; } if (result_Solution2) { cout << " Solution2,验证结果: passed." << endl; } else { cout << " Solution2,验证结果: failed." << endl; } if (result_Solution3) { cout << " Solution3,验证结果: passed." << endl; } else { cout << " Solution3,验证结果: failed." << endl; } cout << "--------------- " << testName << " endl ---------------\n" << endl; } // 完全二叉树 // 8 // / \ // 6 10 // / \ / \ // 5 7 9 11 void Test1() { const int length = 7; int preorder[length] = { 8,6,5,7,10,9,11 }; int inorder[length] = { 5,6,7,8,9,10,11 }; int empty = -1; BinaryTreeNode<int>* pRoot = ConstructTree(preorder, inorder, length); BinaryTreeNode<int>* pMirror = ConstructTree(preorder, inorder, length); MirrorOfBinaryTree(pMirror); test("Test1", pRoot, pMirror, empty); } // 普通二叉树 // a // / \ // b c // / \ / \ // d e f g // / \ // h i void Test2() { const int length = 9; char preorder[length + 1] = "abdehicfg"; char inorder[length + 1] = "dbheiafcg"; char empty = ' '; BinaryTreeNode<char>* pRoot = ConstructTree(preorder, inorder, length); BinaryTreeNode<char>* pMirror = ConstructTree(preorder, inorder, length); MirrorOfBinaryTree(pMirror); test("Test2", pRoot, pMirror, empty); } // 所有结点都没有右子结点 // 1 // / // 2 // / // 3 // / // 4 // / // 5 void Test3() { const int length = 5; char preorder[length + 1] = "12345"; char inorder[length + 1] = "54321"; char empty = '-'; BinaryTreeNode<char>* pRoot = ConstructTree(preorder, inorder, length); BinaryTreeNode<char>* pMirror = ConstructTree(preorder, inorder, length); MirrorOfBinaryTree(pMirror); test("Test3", pRoot, pMirror, empty); } // 所有结点都没有左子结点 // 1 // \ // 2 // \ // 3 // \ // 4 // \ // 5 void Test4() { const int length = 5; char preorder[length + 1] = "12345"; char inorder[length + 1] = "12345"; char empty = '-'; BinaryTreeNode<char>* pRoot = ConstructTree(preorder, inorder, length); BinaryTreeNode<char>* pMirror = ConstructTree(preorder, inorder, length); MirrorOfBinaryTree(pMirror); test("Test4", pRoot, pMirror, empty); } NAMESPACE_MIRROROFBINARYTREEEND // 测试 用例 END // void MirrorOfBinaryTree_Test() { NAME_MIRROROFBINARYTREE::Test1(); NAME_MIRROROFBINARYTREE::Test2(); NAME_MIRROROFBINARYTREE::Test3(); NAME_MIRROROFBINARYTREE::Test4(); } **执行结果:** ![watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L25pZTIzMTQ1NTA0NDE_size_16_color_FFFFFF_t_70][] [20200517230344591.png]: /images/20230312/1a5295ef37534cb58b8f4dfe58b28311.png [Link 1]: https://blog.csdn.net/nie2314550441/article/details/106189949 [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L25pZTIzMTQ1NTA0NDE_size_16_color_FFFFFF_t_70]: /images/20230312/da3f36913ab944be86c549e6f6e9a3da.png
相关 二叉树镜像 文章目录 题目描述 代码 题目描述 操作给定的二叉树,将其变换为源二叉树的镜像。 输入描述: 二叉树的镜像定义: 源二叉树 ╰+攻爆jí腚メ/ 2024年02月19日 13:43/ 0 赞/ 160 阅读
相关 二叉树的镜像 一、前言 《剑指Offer》中题27 二、题目 请完成一个函数,输入一棵二叉树,该函数输出它的镜像。叉树节点的定义如下: ![20200517230344591 Love The Way You Lie/ 2023年03月14日 13:14/ 0 赞/ 33 阅读
相关 二叉树的镜像 剑指offer面试题19:请完成一个函数,输入一个二叉树,该函数输出它的镜像 void MirrorRecursively(BinaryTreeNode pHead) àì夳堔傛蜴生んèń/ 2022年06月17日 05:57/ 0 赞/ 246 阅读
相关 二叉树的镜像 ![这里写图片描述][70] class TreeNode { int val = 0; TreeNode left = null; 小鱼儿/ 2022年05月25日 00:04/ 0 赞/ 273 阅读
相关 二叉树的镜像 ![这里写图片描述][70] class TreeNode { int val = 0; TreeNode left = null; 浅浅的花香味﹌/ 2022年05月24日 22:36/ 0 赞/ 241 阅读
相关 二叉树的镜像 题目描述 操作给定的二叉树,将其变换为源二叉树的镜像。 输入描述: 二叉树的镜像定义: 源二叉树 Love The Way You Lie/ 2022年05月14日 04:13/ 0 赞/ 282 阅读
相关 二叉树的镜像 [二叉树的镜像][Link 1] 题目描述 操作给定的二叉树,将其变换为源二叉树的镜像。 思路: 思路很明了,设置一个新结点,左右孩子交换,递归下去。 柔光的暖阳◎/ 2022年03月25日 15:26/ 0 赞/ 324 阅读
相关 二叉树的镜像 时间限制:1秒 空间限制:32768K 热度指数:221841 算法知识视频讲解 题目描述 操作给定的二叉树,将其变换为源二叉树的镜像。 输入描述: 二叉树的 àì夳堔傛蜴生んèń/ 2022年03月10日 01:37/ 0 赞/ 289 阅读
还没有评论,来说两句吧...