剑指offer-树

原创 2016年08月30日 13:09:55
#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:二叉搜索树与双向链表(java)

题目:输入一颗二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建新的结点,只能调整树中结点指针的指向。 比如如下图中的二叉搜索树,则输出转换之后的排序双向链表为:     由于要...

剑指offer书籍+源码

  • 2017年10月27日 20:52
  • 29.17MB
  • 下载

剑指offer-重建二叉树

题目描述 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4...

牛客--剑指offer-二叉搜索树和双向链表

一、问题描述 输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的指向。 二、解题思路 将二叉搜索树转换成双向链表,其实就是按照中序遍历的方式来...

剑指Offer 最新版

  • 2017年09月11日 10:38
  • 104.53MB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:剑指offer-树
举报原因:
原因补充:

(最多只允许输入30个字)