实现二叉树先序,中序和后序遍历

实现二叉树先序,中序和后序遍历

题目描述

分别按照二叉树先序,中序和后序打印所有的节点。

输入描述:

第一行输入两个整数 n 和 root,n 表示二叉树的总节点个数,root 表示二叉树的根节点。

以下 n 行每行三个整数 fa,lch,rch,表示 fa 的左儿子为 lch,右儿子为 rch。(如果 lch 为 0 则表示 fa 没有左儿子,rch同理)

输出描述:

输出三行,分别表示二叉树的先序,中序和后序。

示例1
输入
3 1
1 2 3
2 0 0
3 0 0
输出
1 2 3
2 1 3
2 3 1
备注:

1 ≤ n ≤ 1 0 6 1 \leq n \leq 10^6 1n106

1 ≤ r o o t , f a , l c h , r c h ≤ n 1 \leq root,fa,lch,rch \leq n 1root,fa,lch,rchn


题解:

先序遍历:根 左 右

中序遍历:左 根 右

后序遍历:左 右 根

先、中、后序遍历按照上面的顺序进行即可。此题如果使用递归,那就非常简单了。鉴于自己对递归转非递归一直未能掌握,此处使用非递归版本代码。稍微麻烦点的是中序遍历,但后续遍历非常麻烦。。。

#include <cstdio>
#include <stack>

using namespace std;

struct BST{
    int val;
    struct BST * lch, * rch;
    BST() {}
    BST(int v) : val(v), lch(NULL), rch(NULL) {}
};

void createTree(BST* root) {
    int fa, lch, rch;
    scanf("%d %d %d", &fa, &lch, &rch);
    if (lch) {
        root->lch = new BST(lch);
        createTree(root->lch);
    }
    if (rch) {
        root->rch = new BST(rch);
        createTree(root->rch);
    }
    return;
}

void preorder(BST* root) {
    if (!root) return;
    stack<BST*> buff;
    buff.push(root);
    while (!buff.empty()) {
        root = buff.top();
        buff.pop();
        printf("%d ", root->val);
        if (root->rch) buff.push(root->rch);
        if (root->lch) buff.push(root->lch);
    }
    puts("");
}

void inorder(BST* root) {
    if (!root) return;
    stack<BST*> buff;
    while (!buff.empty() || root) {
        if (root) {
            buff.push(root);
            root = root->lch;
        } else {
            root = buff.top();
            buff.pop();
            printf("%d ", root->val);
            root = root->rch;
        }
    }
    puts("");
}

void postorder(BST* root) {
    if (!root) return;
    stack<BST*> buff;
    buff.push(root);
    BST *tmp = NULL;
    while (!buff.empty()) {
        tmp = buff.top();
        if (tmp->lch && root != tmp->lch && root != tmp->rch) {
            buff.push(tmp->lch);
        } else if (tmp->rch && root != tmp->rch) {
            buff.push(tmp->rch);
        } else {
            printf("%d ", tmp->val);
            buff.pop();
            root = tmp;
        }
    }
    puts("");
}

int main(void) {
    int n, rt;
    scanf("%d%d", &n, &rt);
    BST* root = new BST(rt);
    createTree(root);
    preorder(root);
    inorder(root);
    postorder(root);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是用 C 语言建立二叉树实现先序中序后序遍历的代码: ```c #include <stdio.h> #include <stdlib.h> //定义二叉树节点结构体 typedef struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; } TreeNode; //创建二叉树 TreeNode* createTree() { TreeNode *root = NULL; int val; scanf("%d", &val); if (val != -1) { //输入-1表示该节点为空 root = (TreeNode*)malloc(sizeof(TreeNode)); root->val = val; root->left = createTree(); root->right = createTree(); } return root; } //先序遍历 void preOrder(TreeNode *root) { if (root != NULL) { printf("%d ", root->val); preOrder(root->left); preOrder(root->right); } } //中序遍历 void inOrder(TreeNode *root) { if (root != NULL) { inOrder(root->left); printf("%d ", root->val); inOrder(root->right); } } //后序遍历 void postOrder(TreeNode *root) { if (root != NULL) { postOrder(root->left); postOrder(root->right); printf("%d ", root->val); } } int main() { printf("请输入二叉树先序遍历,-1表示该节点为空:\n"); TreeNode *root = createTree(); printf("二叉树先序遍历为:"); preOrder(root); printf("\n"); printf("二叉树中序遍历为:"); inOrder(root); printf("\n"); printf("二叉树后序遍历为:"); postOrder(root); printf("\n"); return 0; } ``` 输入格式: 先序遍历的每个节点值,-1表示该节点为空。 输出格式: 二叉树先序中序后序遍历结果。 示例: 输入: ``` 1 2 -1 -1 3 4 -1 -1 5 -1 -1 ``` 输出: ``` 二叉树先序遍历为:1 2 3 4 5 二叉树中序遍历为:2 1 4 3 5 二叉树后序遍历为:2 4 5 3 1 ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值