二叉树反转常被拿来做面试题,所以掌握它是一件必须的事。
下图是二叉树的反转,看起来就像是镜像一样。
首先我们先要构建二叉树的类型
#include <iostream>
using namespace std;
typedef char ElemType;
typedef struct node {
ElemType data; //数据域
struct node* left;
struct node* right; //结点的左右子树指针
} BTNode; //二叉树结点类型
//初始化空二叉树
void InitBTree(BTNode*& root);
//按照前序遍历序列建立二叉树
void CreateBTree_Pre(BTNode*& root, ElemType Array[]);
//按照中序遍历序列建立二叉树
void InOrder(BTNode* root);
//实现二叉树的反转函数
BTNode* InvertBiTree(BTNode* root);
实现基本的功能
#include "BiTree.h"
#include <queue>
//初始化空二叉树