浙大数据结构:03-树3 Tree Traversals Again

这道题也不算难,我依然采用map来进行处理 ,代码依旧较短
机翻

1、条件准备

我这里采用数组模拟栈,tt指向栈顶;
map的键存结点值,后面数对存左右子树的结点值
head存头节点的值
cpp 复制代码
#include<iostream>
#include<map>
#include<string>
using namespace std;

int stk[100],tt=-1;
map<int,pair<int,int>> m;
int head;
主函数先是加快输入输出,然后输入结点数量,
调用inordertraval生成这样一棵树,也就是中序遍历的逆过程
再调用aftertraval后序遍历输出
cpp 复制代码
int main()
{
 ios::sync_with_stdio(false);
 cin.tie(0),cout.tie(0);
 int num;cin>>num;
 inordertraval(num);
  aftertraval(head);
  return 0;
}

2、inordertrava函数

循环遍历进行操作,n个结点有2*n个操作。
用字符串判断是push还是pop。
如果是push,则用f存一下之前同样的位置有没有存结点。
然后将当前结点存进来,如果i==1,则为头结点,存入head.
那么如何连接这些结点呢,我们来分析一下,如果上一个结点左子树为空则连接到该结点左子树。
因为(栈中)上一个结点左子树为空还没弹出栈,则目前连的结点一定是它的左结点。
如果上一个结点左子树连了,则该节点要连之前该位置结点的右子树。
为什么呢?
因为如果要连上一个结点右子树,则该节点应该被弹出,而现在没弹出,相悖了。
而之前该位置结点已经弹出了,证明其左子树遍历完了,该遍历它右子树了,
所以连它的右结点即可。
pop就简单了,tt--即可。
cpp 复制代码
void inordertraval(int n)
{
  n *= 2;
  for (int i = 1; i <= n; i++)
  {
    string operation;
    cin >> operation;
    if (operation == "Push")
    {
      int f = stk[++tt];
      cin >> stk[tt];
      if (i == 1)
        head = stk[tt];
      if (i && m[stk[tt - 1]].first == 0)
        m[stk[tt - 1]].first = stk[tt];
      else 
        m[f].second = stk[tt];
    }
    else
      tt--;
  }
}

3、aftertraval函数

使用递归实现后续遍历,f的左右是控制输出格式,保证最后无多余空格。
如果该节点不为空,则递归左子树,再递归右子树,然后输出该结点。
cpp 复制代码
int f = 1;
void aftertraval(int node)
{
  if (node)
  {
    aftertraval(m[node].first);
    aftertraval(m[node].second);
    if (f)
    {
      cout << node;
      f = 0;
    }
    else
      cout << ' ' << node;
  }
}

4、总结

这个题并不难,map我也只是当作一种结构体在用,并且能find的结构体,较为方便。
完整代码如下:
cpp 复制代码
#include <iostream>
#include <map>
#include <string>
using namespace std;

int stk[100], tt = -1;
map<int, pair<int, int>> m;
int head;

void inordertraval(int n)
{
  n *= 2;
  for (int i = 1; i <= n; i++)
  {
    string operation;
    cin >> operation;
    if (operation == "Push")
    {
      int f = stk[++tt];
      cin >> stk[tt];
      if (i == 1)
        head = stk[tt];
      if (i && m[stk[tt - 1]].first == 0)
        m[stk[tt - 1]].first = stk[tt];
      else if (i && m[f].second == 0)
        m[f].second = stk[tt];
    }
    else
      tt--;
  }
}

int f = 1;
void aftertraval(int node)
{
  if (node)
  {
    aftertraval(m[node].first);
    aftertraval(m[node].second);
    if (f)
    {
      cout << node;
      f = 0;
    }
    else
      cout << ' ' << node;
  }
}
int main()
{
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  int num;
  cin >> num;
  inordertraval(num);
  aftertraval(head);
  return 0;
}
相关推荐
xlcoding39 分钟前
位掩码、哈希表、异或运算、杨辉三角、素数查找、前缀和
c++·算法·蓝桥杯
alive9031 小时前
【QT】 进程
c++·qt·嵌入式·进程·qprocess
小开不是小可爱2 小时前
leetcode_383. 赎金信_java
java·数据结构·算法·leetcode
√尖尖角↑5 小时前
力扣——【1991. 找到数组的中间位置】
算法·蓝桥杯
Allen Wurlitzer5 小时前
算法刷题记录——LeetCode篇(1.8) [第71~80题](持续更新)
算法·leetcode·职场和发展
百锦再7 小时前
五种常用的web加密算法
前端·算法·前端框架·web·加密·机密
刚入门的大一新生8 小时前
C++初阶-C++入门基础
开发语言·c++
碳基学AI8 小时前
北京大学DeepSeek内部研讨系列:AI在新媒体运营中的应用与挑战|122页PPT下载方法
大数据·人工智能·python·算法·ai·新媒体运营·产品运营
独家回忆3649 小时前
每日算法-250410
算法
袖清暮雨9 小时前
Python刷题笔记
笔记·python·算法