# 剑指offer-树

#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std;
struct BinaryTreeNode
{
int data;
BinaryTreeNode *left;
BinaryTreeNode *right;
};
BinaryTreeNode *InsertNode(BinaryTreeNode *left, BinaryTreeNode *right, int data){
BinaryTreeNode *root=new BinaryTreeNode;
root->left=left;
root->right=right;
root->data=data;
return root;
}
//按层次打印二叉树   递归和非递归版本
void  printByLevel(BinaryTreeNode *root){
queue<BinaryTreeNode *>  q;
q.push(root);
int i=1;
while (!q.empty())
{
BinaryTreeNode *p = q.front();
q.pop();
cout <<i++<< " "<<p->data << endl;
if (p->left)
q.push(p->left);
if (p->right)
q.push(p->right);
}
}
void  printByLevelTraverse(BinaryTreeNode *root,int i){
cout << i << ":" << root->data << endl;
if (root->left)
printByLevelTraverse(root->left,2*i);
if (root->right)
printByLevelTraverse(root->right,2*i+1);
}
//先序遍历
void PreOrder(BinaryTreeNode *root){
if (root)
{
cout << root->data << " ";
PreOrder(root->left);
PreOrder(root->right);
}
}
void PreOrderTraverse(BinaryTreeNode *root){
stack<BinaryTreeNode *> s;
BinaryTreeNode *p=root;
while (p||!s.empty())
{
while (p)
{
cout << p->data << " ";
s.push(p);
p=p->left;
}
if (!s.empty()){
p = s.top();
s.pop();
p=p->right;
}
}
cout<<endl;
}
//中序遍历
void MidOrder(BinaryTreeNode *root){
if (root){
MidOrder(root->left);
cout << root->data << " ";
MidOrder(root->right);
}
}
//递归中序遍历
void MidOrderTraverse(BinaryTreeNode *root){
stack<BinaryTreeNode *> s;
BinaryTreeNode *p=root;
while (p||!s.empty())
{
while (p)
{
s.push(p);
p=p->left;
}
if (!s.empty()){
p = s.top();
s.pop();
cout << p->data << " ";
p=p->right;
}
}
cout<<endl;
}
//后序遍历
void PostOrder(BinaryTreeNode *root){
if (root){
PostOrder(root->left);
PostOrder(root->right);
cout << root->data << " ";
}
}
void PostOrderTraverse(BinaryTreeNode *root){
BinaryTreeNode *p=root;
BinaryTreeNode *q=NULL;
stack<BinaryTreeNode *>  s;
while (p||!s.empty())
{
while (p)
{
s.push(p);
p=p->left;
}
if (!s.empty()){
p = s.top();
if (p->right == NULL||p->right==q){
cout << p->data << " ";
q=p;
p=NULL;
s.pop();
}
else{
p=p->right;
}
}
}
cout<<endl;
}
//二叉树的深度
int DepthOfBinaryTree(BinaryTreeNode *root){
if (root==NULL)
return 0;
int l=DepthOfBinaryTree(root->left);
int r=DepthOfBinaryTree(root->right);
return l>r?l+1:r+1;
}

//根据前序遍历和中序遍历重建二叉树
BinaryTreeNode *ConstructCore(int *startPreOrder, int *endPreOrder, int *startMidOrder, int *endMideOrder){
int rootValue = startPreOrder[0];
BinaryTreeNode *root = new BinaryTreeNode();
root->data=rootValue;
root->left=NULL;
root->right=NULL;

//递归结束条件
if (startPreOrder == endPreOrder){
if (startMidOrder==endMideOrder&&startPreOrder==startMidOrder)
return root;
else
throw exception("inValid input");
}
//在中序遍历中找到根结点的值
int *rootInOrder=startMidOrder;
while (rootInOrder<=endMideOrder&&*rootInOrder!=rootValue)
{
++rootInOrder;
}
int leftLength=rootInOrder-startMidOrder;
int *leftPreOrderEnd=startPreOrder+leftLength;
if (leftLength > 0){
//构建左子树
root->left = ConstructCore(startPreOrder+1,leftPreOrderEnd,startMidOrder,rootInOrder-1);
}
if (leftLength < endPreOrder - startMidOrder){
//构建右子树
root->right = ConstructCore(leftPreOrderEnd+1,endPreOrder,rootInOrder+1,endMideOrder);
}
return root;
}
BinaryTreeNode *Construct(int *preOrder, int *midOrder,int n){
if (preOrder==NULL||midOrder==NULL||n<=0)
return NULL;
return ConstructCore(preOrder,preOrder+n-1,
midOrder,midOrder+n-1);
}
//打印二叉树中和为某一值的路径
void  PrintTreePathOfTarget(BinaryTreeNode *root, int sum, int target, vector<int> &pathVec)
{
sum+=root->data;
pathVec.push_back(root->data);
bool isLeaf = root->left == NULL&& root->right == NULL;
if (sum == target&&isLeaf)
{
for (auto c:pathVec)
cout << c << " ";
cout<<endl;
}
if (root->left){
PrintTreePathOfTarget(root->left,sum,target,pathVec);
}
if (root->right)
{
PrintTreePathOfTarget(root->right,sum,target,pathVec);
}
pathVec.pop_back();
}

//二叉树的镜像
void MirrorOfTree(BinaryTreeNode *root){
if (root==NULL)  return ;
if (root->left==NULL&&root->right==NULL)
return;
BinaryTreeNode *p = root->left;
root->left = root->right;
root->right = p;
if (root->left)
MirrorOfTree(root->left);
if (root->right)
MirrorOfTree(root->right);
}

//输入两颗二叉树A和B，判断B是不是A的子结构

bool isSubCore(BinaryTreeNode *A, BinaryTreeNode *B){
if (A != NULL&&B != NULL){
if (A->data == B->data){
return isSubCore(A->left,B->left)&&isSubCore(A->right,B->right);
}
}
if (A==NULL&&B==NULL)    return true;
return false;
}
bool isSubTree(BinaryTreeNode *A, BinaryTreeNode *B)
{
bool result=false;
if (A->data==B->data)
result = isSubCore(A,B);
if (!result){
result = isSubTree(A->left,B);
}
if (!result){
result = isSubTree(A->right,B);
}
}

int main(){
//1.创建二叉树
BinaryTreeNode *n1 = InsertNode(NULL,NULL,1);
BinaryTreeNode *n2 = InsertNode(NULL,NULL,7);
BinaryTreeNode *n3 = InsertNode(NULL, NULL, 8);
BinaryTreeNode *n4 = InsertNode(NULL, NULL, 3);
BinaryTreeNode *n5 = InsertNode(n1, n2, 4);
BinaryTreeNode *n6 = InsertNode(n3, n4, 5);
BinaryTreeNode *root = InsertNode(n5,n6,2);
//2.按层次打印二叉树
printByLevel(root);     //迭代
printByLevelTraverse(root,1);//递归

//3.先序遍历
PreOrder(root);cout<<endl;
PreOrderTraverse(root);

//4.中序遍历
MidOrder(root);cout<<endl;
MidOrderTraverse(root);

//5.后序遍历
PostOrder(root);cout<<endl;
PostOrderTraverse(root);

//6.返回二叉树的深度
cout << DepthOfBinaryTree(root)<<endl;

//7.根据前序遍历和中序遍历重建二叉树
//const int n=7;
//int  preOrder[n] = {2,4,1,7,5,8,3};//已知前序遍历
//int  midOrder[n] = {1,4,7,2,8,5,3};//已知中序遍历
//Construct(preOrder,midOrder,n);

//8.打印和为某一值的路径
vector<int>  pathVec;
PrintTreePathOfTarget(root,0,15,pathVec);

//9.二叉树的镜像
//    MirrorOfTree(root);
//    printByLevel(root);

//10.输入两颗二叉树A和B，判断B是不是A的子结构
BinaryTreeNode *A=root;
BinaryTreeNode *B=n5;
bool flag= isSubTree(A,B);

cout<<flag<<endl;
system("pause");
return 0;
}

#### 【剑指Offer学习】【面试题4 ： 替换空格】

2015-04-28 08:24:29

#### 剑指Offer-名企面试官精讲面试试题

2015年05月07日 21.46MB 下载

#### 剑指offer-(java版-2).pdf

2018年03月21日 469KB 下载

#### 【剑指offer】变态跳台阶

2014-05-09 09:52:08

#### 剑指Offer面试题4（Java版），替换空格

2015-07-24 17:58:17

#### 剑指offer----树的子结构----java实现

2016-07-07 09:36:13

#### 剑指Offer-树的子结构

2015-08-29 00:11:44

#### 剑指offer-树的子结构

2017-04-01 14:22:11

#### *剑指offer-树的子结构

2018-05-30 15:03:54

#### 剑指offer——矩形覆盖问题

2016-07-30 10:35:05