树的遍历【东北大学oj数据结构7-3】C++

题面

二叉树是递归定义的。 二叉树 T 是定义在有限节点集上的结构

不包含节点,或者由三个不相交的节点集组成:

  • 一个根节点。
  • 称为左子树的二叉树。
  • 称为右子树的二叉树。
    您的任务是编写一个程序,该程序基于以下算法执行树遍历(系统地遍历树中的所有节点):

打印根、左子树和右子树(前序)。

打印左子树、根和右子树(中序)。

打印左子树、右子树和根(后序)。

这里,给定的二叉树由 n 个节点组成,每个节点都有一个从0到n-1的唯一ID。

输入

输入的第一行包括一个整数 n,即树的节点数。

在接下来的 n 行中,每个节点的信息以以下格式给出:

id left right

id 是节点的ID,left 是左孩子的ID,right 是右孩子的ID。 如果节点没有左(右)子节点,左(右)用-1表示

输出

在第一行,打印"Preorder",在第二行,打印通过前序遍历获得的节点 ID 列表。

在第 3 行打印"Inorder",在第 4 行打印通过中序遍历获得的节点 ID 列表。

在第 5 行打印"Postorder",在第 6 行打印通过后序遍历获得的节点 ID 列表。

在每个节点 ID 之前打印一个空格字符。

Constraints

1 ≤ n ≤ 25

输入样例

9

0 1 4

1 2 3

2 -1 -1

3 -1 -1

4 5 8

5 6 7

6 -1 -1

7 -1 -1

8 -1 -1

输出样例

Preorder

0 1 2 3 4 5 6 7 8

Inorder

2 1 3 0 6 5 7 4 8

Postorder

2 3 1 6 7 5 8 4 0

递归实现
cpp 复制代码
#include <iostream>
 
using namespace std;
 
#define MAX 30
 
// 定义树的节点结构
struct Node {
    int p, l, r;
};
 
struct Node T[MAX];  // 树节点数组
int n;  // 节点的数量
 
void preorder(int root)
{
    if(root==-1) return;
    cout<<root<<" ";
    preorder(T[root].l);
    preorder(T[root].r);
}
void inorder(int root)
{
    if(root==-1) return;
    inorder(T[root].l);
    cout<<root<<" ";
    inorder(T[root].r);
}
void posorder(int root)
{
    if(root==-1) return;
    posorder(T[root].l);
    posorder(T[root].r);
    cout<<root<<" ";
}
 
int main() {
    int i, v, l, r;
    scanf("%d", &n);
    for (i = 0; i < n; i++) {
        T[i].p = T[i].l = T[i].r = -1;
    }
    for (i = 0; i < n; i++) {
        scanf("%d %d %d", &v, &l,&r);
        if(l!=-1)
        {
            T[v].l=l;
            T[l].p=v;
        }
        if(r!=-1)
        {
            T[v].r=r;
            T[r].p=v;
        }
    }
 
    for (int i = 0; i < n; i++) {
        if(T[i].p==-1)
        {
            r=i;
            break;
        }
    }
 
    cout<<"Preorder"<<endl;
    preorder(r);
    cout<<endl;
    cout<<"Inorder"<<endl;
    inorder(r);
    cout<<endl;
    cout<<"Postorder"<<endl;
    posorder(r);
    cout<<endl;
 
    return 0;
}
非递归实现
cpp 复制代码
#include <iostream>
#include <stack>
using namespace std;
 
#define MAX 30
 
// 定义树的节点结构
struct Node {
    int p, l, r;
};
 
struct Node T[MAX];  // 树节点数组
int n;  // 节点的数量
 
void preorder(int root)
{
    if(root==-1) return;
    stack<int> s;
    s.push(root);
    while(!s.empty())
    {
        int node=s.top();
        s.pop();
        cout<<node<<" ";
        if(T[node].r!=-1)
            s.push(T[node].r);
        if(T[node].l!=-1)
            s.push(T[node].l);
    }
}
void inorder(int root)
{
    if(root==-1) return;
    stack<int> s;
    int current=root;
    while(!s.empty()||current!=-1)
    {
        while(current!=-1)
        {
            s.push(current);
            current=T[current].l;
        }
        current=s.top();
        s.pop();
        cout<<current<<" ";
        current=T[current].r;
    }
}
void posorder(int root)
{
    if(root==-1) return;
    stack<int> s1,s2;
    s1.push(root);
    while(!s1.empty())
    {
        int node=s1.top();
        s1.pop();
        s2.push(node);
        if(T[node].l!=-1)
            s1.push(T[node].l);
        if(T[node].r!=-1)
            s1.push(T[node].r);
    }
    while(!s2.empty())
    {
        cout<<s2.top()<<" ";
        s2.pop();
    }
}
 
int main() {
    int i, v, l, r;
    scanf("%d", &n);
    for (i = 0; i < n; i++) {
        T[i].p = T[i].l = T[i].r = -1;
    }
    for (i = 0; i < n; i++) {
        scanf("%d %d %d", &v, &l,&r);
        if(l!=-1)
        {
            T[v].l=l;
            T[l].p=v;
        }
        if(r!=-1)
        {
            T[v].r=r;
            T[r].p=v;
        }
    }
 
    for (int i = 0; i < n; i++) {
        if(T[i].p==-1)
        {
            r=i;
            break;
        }
    }
 
    cout<<"Preorder"<<endl;
    preorder(r);
    cout<<endl;
    cout<<"Inorder"<<endl;
    inorder(r);
    cout<<endl;
    cout<<"Postorder"<<endl;
    posorder(r);
    cout<<endl;
 
    return 0;
}
相关推荐
feiyangqingyun18 分钟前
Qt/C++开发监控GB28181系统/录像文件查询/录像回放/倍速播放/录像文件下载
c++·qt·gb28181·录像回放·录像文件下载
2301_8076114932 分钟前
310. 最小高度树
c++·算法·leetcode·深度优先·回溯
为自己_带盐1 小时前
浅聊一下数据库的索引优化
开发语言·数据库·php
明月看潮生1 小时前
青少年编程与数学 02-019 Rust 编程基础 12课题、所有权系统
开发语言·青少年编程·rust·编程与数学
四谷夕雨1 小时前
C++八股——智能指针
c++
shengjk12 小时前
序列化和反序列化:从理论到实践的全方位指南
java·大数据·开发语言·人工智能·后端·ai编程
学习中的码虫2 小时前
数据结构中的高级排序算法
数据结构·算法·排序算法
passionSnail2 小时前
《用MATLAB玩转游戏开发》推箱子游戏的MATLAB趣味实现
开发语言·游戏·matlab
Once_day2 小时前
C++之fmt库介绍和使用(1)
开发语言·c++·fmt
是店小二呀2 小时前
【优选算法 | 字符串】字符串模拟题精选:思维+实现解析
android·c++·算法