2.3a 二叉树的先中后序遍历

2.3a 二叉树的先中后序遍历_哔哩哔哩_bilibili 

 

//二叉树的递归遍历
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>

//二叉树结点定义
typedef struct BiTNode{
    char data; //数据域
    struct BiTNode *Left;  //左孩子指针
    struct BiTNode *Right; //右孩子指针
}BiTNode,*BiTree; //结构类型,结构的指针类型

//先序遍历
void PreOrder(BiTree T){
    if(T==NULL) return;
    printf("%c ",T->data);
    PreOrder(T->Left);
    PreOrder(T->Right);
}

//中序遍历
void InOrder(BiTree T){
    if(T==NULL) return;
    InOrder(T->Left);
    printf("%c ",T->data);
    InOrder(T->Right);
}

//后序遍历
void PostOrder(BiTree T){
    if(T==NULL) return;
    PostOrder(T->Left);
    PostOrder(T->Right);
    printf("%c ",T->data);
}

//用串先序递归构建二叉树
int sc=0; //全局变量sc, 枚举串
BiTree CreateBiTree(char *s){ //s为先序遍历串
    BiTree T;
    if(s[sc]=='^') T=NULL;
    else{
        T=(BiTree)malloc(sizeof(BiTNode));
        T->data=s[sc++];
        T->Left=CreateBiTree(s); sc++;
        T->Right=CreateBiTree(s);
    }
    return T;
}

int main(){
    char s[]="ABD^^EG^^^CF^H^^^";
    BiTree T=CreateBiTree(s);
    
    printf("先序递归:"); PreOrder(T);
    printf("\n中序递归:"); InOrder(T);
    printf("\n后序递归:"); PostOrder(T);
    return 0;
}

 

//二叉树的非递归遍历 C++编译
#include<stdio.h>
#include<stdlib.h>

//二叉树结点定义
typedef struct BiTNode{
    char data; //数据域
    struct BiTNode *lchild; //左孩子指针
    struct BiTNode *rchild; //右孩子指针
}BiTNode,*BiTree; //结构类型,结构的指针类型

// ========== 非递归遍历(栈实现,考研重点) ==========
#define MAXN 100
// 中序非递归(最常考)
void InOrderNoRec(BiTree T) {
    BiTNode *Stack[MAXN];
    int top = 0;
    BiTNode *p = T;
    while (p != NULL || top != 0) {
        while (p != NULL) {   // 一直走到最左
            Stack[++top] = p;
            p = p->lchild;
        }
        p = Stack[top--];    // 出栈访问
        printf("%c ", p->data);
        p = p->rchild;       // 转向右子树
    }
}

// 先序非递归
void PreOrderNoRec(BiTree T) {
    BiTNode *Stack[MAXN];
    
    int top = 0;
    BiTNode *p = T;
    if (p == NULL) return;
    Stack[++top] = p;
    while (top != 0) {
        p = Stack[top--];
        printf("%c ", p->data);
        if (p->rchild) Stack[++top] = p->rchild; // 右先入栈,后访问
        if (p->lchild) Stack[++top] = p->lchild;
    }
}

// 后序非递归(双标记法)
void PostOrderNoRec(BiTree T) {
    typedef struct {
        BiTNode *node;
        int flag; // 0未访问,1已访问孩子
    } StackNode;
    StackNode Stack[MAXN];
    
    int top = 0;
    BiTNode *p = T;
    if (p == NULL) return;
    Stack[++top] = {p,0};
    while(top>0){
        StackNode cur = Stack[top--];
        if(cur.flag == 0){
            Stack[++top] = {cur.node,1};
            if(cur.node->rchild) Stack[++top] = {cur.node->rchild,0};
            if(cur.node->lchild) Stack[++top] = {cur.node->lchild,0};
        }else{
            printf("%c ", cur.node->data);
        }
    }
}

//用串先序递归构建二叉树
int sc=0; //全局变量sc, 枚举串
BiTree CreateBiTree(char *s){ //s为先序遍历串
    BiTree T;
    if(s[sc]=='^') T=NULL;
    else{
        T=(BiTree)malloc(sizeof(BiTNode));
        T->data=s[sc++];
        T->lchild=CreateBiTree(s); sc++;
        T->rchild=CreateBiTree(s);
    }
    return T;
}

int main(){
    char s[]="ABD^^EG^^^CF^H^^^";
    BiTree T=CreateBiTree(s);
    
    printf("先序非递归:"); PreOrderNoRec(T);
    printf("\n中序非递归:"); InOrderNoRec(T);
    printf("\n后序非递归:"); PostOrderNoRec(T);
    return 0;
}

 

144. 二叉树的前序遍历 - 力扣  94. 二叉树的中序遍历 - 力扣        145. 二叉树的后序遍历 - 力扣

//c
void preorder(struct TreeNode* root, int* res, int* siz) {
    if (root == NULL) {
        return;
    }
    res[(*siz)++] = root->val;
    preorder(root->left, res, siz);
    preorder(root->right, res, siz);
}
int* preorderTraversal(struct TreeNode* root, int* returnSize) {
    int* res = malloc(sizeof(int)*2000);
    *returnSize = 0;
    preorder(root, res, returnSize);
    return res;
}
//C++
class Solution {
public:
    vector<int> t; //存储序列
    void preorder(TreeNode* root){ //前序遍历
        if(root==NULL)return;
        t.push_back(root->val);
        preorder(root->left);
        preorder(root->right);
    }
    vector<int> preorderTraversal(TreeNode* root){     
        preorder(root);
        return t; //返回序列
    }
};

 

104. 二叉树的最大深度 - 力扣(LeetCode)

//c 类似后序遍历
int maxDepth(struct TreeNode* root) {
    if(root==NULL) return 0;
    int l=maxDepth(root->left);
    int r=maxDepth(root->right);
    return fmax(l,r)+1;
}
//c++
class Solution {
public:
    int maxDepth(TreeNode* root) {
        if(!root) return 0;
        return max(maxDepth(root->left),maxDepth(root->right))+1;
    }
};

 

222. 完全二叉树的节点个数 - 力扣(LeetCode)

//c 类似后序遍历
int countNodes(struct TreeNode* root) {
    if(!root) return 0;
    return countNodes(root->left)+countNodes(root->right)+1;
}
//C++
class Solution {
public:
    int countNodes(TreeNode* root){
        if(!root) return 0;
        return countNodes(root->left) + countNodes(root->right) + 1;
    }
};

 

226. 翻转二叉树 - 力扣(LeetCode)

//C 类似后序遍历
struct TreeNode* invertTree(struct TreeNode* root) {
    if(!root) return NULL;

    invertTree(root->left);
    invertTree(root->right);
    struct TreeNode* t;
    t=root->left;
    root->left=root->right;
    root->right=t;
    return root;    
}
//C++
class Solution {
public:
    TreeNode* invertTree(TreeNode* root) {
        if(!root)return nullptr;

        invertTree(root->left);
        invertTree(root->right);
        swap(root->left,root->right); //交换左右指针
        return root;
    }
};

 

posted @ 2026-09-24 07:02  董晓  阅读(15)  评论(0)    收藏  举报