二叉树的遍历

一.前序遍历

    (1)先访问左子树。(2)再访问根节点。(3)最后访问右子树。
 

上面这个二叉树的前序遍历结果是:ABDECFG. 

1.力扣144.二叉树的前序遍历

官方解法:

(1)递归法:

void preorder(struct TreeNode* root, int* res, int* resSize) {
    if (root == NULL) {
        return;
    }
    res[(*resSize)++] = root->val;
    preorder(root->left, res, resSize);
    preorder(root->right, res, resSize);
}

int* preorderTraversal(struct TreeNode* root, int* returnSize) {
    int* res = malloc(sizeof(int) * 2000);
    *returnSize = 0;
    preorder(root, res, returnSize);
    return res;
}

(2)迭代法:

int* preorderTraversal(struct TreeNode* root, int* returnSize) {
    int* res = malloc(sizeof(int) * 2000);
    *returnSize = 0;
    if (root == NULL) {
        return res;
    }

    struct TreeNode* stk[2000];
    struct TreeNode* node = root;
    int stk_top = 0;
    while (stk_top > 0 || node != NULL) {
        while (node != NULL) {
            res[(*returnSize)++] = node->val;
            stk[stk_top++] = node;
            node = node->left;
        }
        node = stk[--stk_top];
        node = node->right;
    }
    return res;
}

二.中序遍历

    (1)先访问左子树。(2)再访问根节点。(3)最后访问右子树。


 

 

上面这个二叉树的中序遍历结果是:DBEAFCG.

2力扣94.二叉树的中序遍历 

官方解法:

(1)递归法

void inorder(struct TreeNode* root, int* res, int* resSize) {
    if (!root) {
        return;
    }
    inorder(root->left, res, resSize);
    res[(*resSize)++] = root->val;
    inorder(root->right, res, resSize);
}

int* inorderTraversal(struct TreeNode* root, int* returnSize) {
    int* res = malloc(sizeof(int) * 501);
    *returnSize = 0;
    inorder(root, res, returnSize);
    return res;
}

(2)迭代法

int* inorderTraversal(struct TreeNode* root, int* returnSize) {
    *returnSize = 0;
    int* res = malloc(sizeof(int) * 501);
    struct TreeNode** stk = malloc(sizeof(struct TreeNode*) * 501);
    int top = 0;
    while (root != NULL || top > 0) {
        while (root != NULL) {
            stk[top++] = root;
            root = root->left;
        }
        root = stk[--top];
        res[(*returnSize)++] = root->val;
        root = root->right;
    }
    return res;
}

三.二叉树的后序遍历

    (1)先访问左子树。(2)再访问右子树。(3)最后访问根节点。


 

 上面这个二叉树的后序遍历结果是:DEBFGCA.

3.力扣145.二叉树的后序遍历

官方解法:

(1)递归法

void postorder(struct TreeNode *root, int *res, int *resSize) {
    if (root == NULL) {
        return;
    }
    postorder(root->left, res, resSize);
    postorder(root->right, res, resSize);
    res[(*resSize)++] = root->val;
}

int *postorderTraversal(struct TreeNode *root, int *returnSize) {
    int *res = malloc(sizeof(int) * 2001);
    *returnSize = 0;
    postorder(root, res, returnSize);
    return res;
}

(2)迭代法

int *postorderTraversal(struct TreeNode *root, int *returnSize) {
    int *res = malloc(sizeof(int) * 2001);
    *returnSize = 0;
    if (root == NULL) {
        return res;
    }
    struct TreeNode **stk = malloc(sizeof(struct TreeNode *) * 2001);
    int top = 0;
    struct TreeNode *prev = NULL;
    while (root != NULL || top > 0) {
        while (root != NULL) {
            stk[top++] = root;
            root = root->left;
        }
        root = stk[--top];
        if (root->right == NULL || root->right == prev) {
            res[(*returnSize)++] = root->val;
            prev = root;
            root = NULL;
        } else {
            stk[top++] = root;
            root = root->right;
        }
    }
    return res;
}

三种遍历方式

typedef struct Tree{
 
 char data;					//	存放数据域
 struct Tree *Lnode;			//	遍历左子树指针
 struct Tree *Rnode;			//	遍历右子树指针

}Tree,*BitTree;
//二叉树的先序遍历
void PreOrderTraverse(BitTree T)
{
    if(T == NULL){
       return;
    }
    printf("%c ",T -> data);
    PreOrderTraverse(T -> Lnode);
    PreOrderTraverse(T -> Rnode);
}

//二叉树的中序遍历
void InOrderTraverse(BitTree T)
{
    if(T == NULL){
       return;
    }
    InOrderTraverse(T -> Lnode);
    printf("%c ",T -> data);
    InOrderTraverse(T -> Rnode);
}

//二叉树的后序遍历
void PostOrderTraverse(BitTree T)
{
    if(T == NULL){ 
       return;
    }
    PostOrderTraverse(T -> Lnode);
    PostOrderTraverse(T -> Rnode);
    printf("%c ",T -> data);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值