【面试题】剑指offer 19
来源:互联网 发布:珠海软件测试培训机构 编辑:程序博客网 时间:2024/06/04 21:54
题目:请完成一个函数,输入一个二叉树,该函数输出它的镜像
代码
#include<iostream>#include<cstdlib>using namespace std;struct BinTreeNode{ int _value; BinTreeNode* _pLeft; BinTreeNode* _pRight; BinTreeNode(int x) :_value(x) ,_pLeft(NULL) ,_pRight(NULL) {}};class BinTree{public: BinTree() :_root(NULL) {} ~BinTree() { delete _root; _root=NULL; } BinTree(int * a,int size,const int& invalid) { int index=0; _createBinTree(_root,a,size,index,invalid); } void Proder() { _Proder(_root); cout<<endl; } void MirrorRecursively() { _MirrorRecursively(_root); }protected: void _MirrorRecursively(BinTreeNode* root) { if (root==NULL) return; if (root->_pLeft==NULL&&root->_pRight==NULL) return; BinTreeNode* tmp=root->_pLeft; root->_pLeft=root->_pRight; root->_pRight=tmp; if(root->_pLeft) _MirrorRecursively(root->_pLeft); if (root->_pRight) _MirrorRecursively(root->_pRight); } void _createBinTree(BinTreeNode*& root,int a[],int size,int &index,int invalid) { if(index<size&&a[index]!=invalid) { root=new BinTreeNode(a[index]); _createBinTree(root->_pLeft,a,size,++index,invalid); _createBinTree(root->_pRight,a,size,++index,invalid); } } void _Proder(BinTreeNode* root) { if(root==NULL) return; cout<<root->_value<<" "; _Proder(root->_pLeft); _Proder(root->_pRight); }private: BinTreeNode* _root;};void test(){ int arr[]={8,6,5,'#','#',7,'#','#',10,9,'#','#',11}; BinTree t1(arr,13,'#'); t1.Proder(); t1.MirrorRecursively(); t1.Proder();}void test2(){ int arr[]={8,6,5,'#','#',7}; BinTree t1(arr,6,'#'); t1.Proder(); t1.MirrorRecursively(); t1.Proder();}
#include "BinTree.h"int main(){ test(); test2(); system("pause"); return 0;}
test所用的二叉树
test2所用二叉树
0 0
- 剑指offer面试题19
- 【面试题】剑指offer 19
- 剑指offer 面试题
- 剑指offer面试题
- 剑指offer 面试题19 二叉树镜像
- 剑指Offer(面试题19~23)
- 剑指Offer(面试题19-20)
- 剑指Offer面试题19 & Leetcode226
- 剑指offer面试题总结
- 剑指offer面试题06
- 剑指offer面试题 04
- 剑指offer 面试题07
- 剑指offer面试题08
- 剑指offer面试题42
- 剑指offer面试题1
- 剑指offer面试题3
- 剑指offer面试题4
- 剑指offer面试题5
- 八大排序算法之冒泡排序
- 点击菜单跳转页面给点击的菜单添加新的样式
- Myeclipse 的优化
- 更改matlab默认工作路径
- ES6——Day2(跨模块常量+全局对象属性)
- 【面试题】剑指offer 19
- LA3942 背单词(trie树&&简单动规)
- 安卓学习(广告栏的实现)
- Java中Lambda表达式的使用
- 查并集基础
- skiing 【DP】
- virtualbox 不识别USB
- 函数指针
- SVN的使用