二叉树经典题解

例题:https://blog.csdn.net/qq_42866708/article/details/81409325
下面是全解;

#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;

struct BiTNode{
    char data;
    struct BiTNode* lchild, * rchild;
};
BiTNode* T;
void CreateTree(BiTNode* &T){
    char ch;
    ch = getchar();
    if(ch != '#'){
        T = new BiTNode;
        T->data = ch;
        CreateTree(T->lchild);
        CreateTree(T->rchild);
    }else{
        T = NULL;
    }
}
void preorder(BiTNode* T){
    if(T){
        cout << T->data;
        preorder(T->lchild);
        preorder(T->rchild);
    }
}

void inorder(BiTNode* T){
    if(T){
        inorder(T->lchild);
        cout << T->data;
        inorder(T->rchild);
    }
}

void postorder(BiTNode* T){
    if(T){
        postorder(T->lchild);
        postorder(T->rchild);
        cout << T->data;
    }
}

void cengci(BiTNode* T){
    queue<BiTNode*> q;
    if(T){
        q.push(T);
        while(!q.empty()){
            T = q.front();
            q.pop();
            cout << T->data;
            if(T->lchild){
                q.push(T->lchild);
            }
            if(T->rchild){
                q.push(T->rchild);
            }
        }
    }
}
int main(){
    CreateTree(T);
    cout << "先序遍历:";
    preorder(T);
    cout << endl << "中序遍历:";
    inorder(T);  
    cout << endl << "后序遍历:";
    postorder(T);
    cout << endl << "层次遍历:";
    cengci(T); 
    return 0;
}

下面是另一种写法;

#include<stdio.h>
#include<stdlib.h>
#include<cstring>
#include<queue>
#include<iostream>
using namespace std;

typedef struct Node{
    struct Node* lchild;//左孩子 
    struct Node* rchild;//右孩子 
    char c;
}Node,*BiTree; 

BiTree creatTree(){//建树 
    char str;
    BiTree T;
    scanf("%c",&str);
    if(str=='#') 
        {
            T=NULL;
        }

    else{
        T = new Node;
        T->c = str;
        T->lchild = creatTree();
        T->rchild = creatTree();
        }
    return T; 
}
//先序遍历
void preOrder(BiTree T){
    if(T){
        printf("%c",T->c);
        preOrder(T->lchild);
        preOrder(T->rchild);
    }
} 
//中序遍历
void inOrder(BiTree T){
    if(T){
        inOrder(T->lchild);
        printf("%c",T->c);
        inOrder(T->rchild);
    }
} 
//后序
void postOrder(BiTree T){
    if(T){
        postOrder(T->lchild);
        postOrder(T->rchild);
        printf("%c",T->c);
    }
} 
 //层次
 void cengorder(BiTree T){
    BiTree P;
     queue <BiTree> Q;
    if(T){
        Q.push(T);
    }
    while(!Q.empty() ){
        P=Q.front() ;
        Q.pop() ;
        printf("%c",P->c );
        if(P->lchild !=NULL){
            Q.push(P->lchild ); 
        }   
        if(P->rchild !=NULL){
            Q.push(P->rchild );     
        }
    } 
  } 
int main(){
    BiTree T;
    T = creatTree();
    preOrder(T);
    inOrder(T);
    postOrder(T);
    cengorder(T);
    return 0;
} 
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值