#P1413. 【Day5】遍历与还原完整程序阅读(12题)

【Day5】遍历与还原完整程序阅读(12题)

程序一:四种遍历共享同一棵树

数组 lcrc 分别保存左右孩子编号,0 表示空。完成第 1~6 题。

int lc[8] = {0,2,4,0,0,7,0,0};
int rc[8] = {0,3,5,6,0,0,0,0};
void pre(int u){ if(!u)return; cout<<u<<" "; pre(lc[u]); pre(rc[u]); }
void in(int u){ if(!u)return; in(lc[u]); cout<<u<<" "; in(rc[u]); }
void post(int u){ if(!u)return; post(lc[u]); post(rc[u]); cout<<u<<" "; }

第 1 题

调用 pre(1) 的输出序列是什么?

{{ input(1) }}


第 2 题

调用 in(1) 的输出序列是什么?

{{ input(2) }}


第 3 题

调用 post(1) 的输出序列是什么?

{{ input(3) }}


第 4 题

层序遍历序列是什么?

{{ input(4) }}


第 5 题

根在第 1 层时,这棵树的深度是多少?

{{ input(5) }}


第 6 题

叶子结点共有多少个?

{{ input(6) }}


情境二:由前序和中序还原

已知前序为 1 2 4 5 7 3 6,中序为 4 2 7 5 1 3 6。完成第 7~12 题。

第 7 题

整棵树的根结点编号是多少?

{{ input(7) }}


第 8 题

左子树共有多少个结点?

{{ input(8) }}


第 9 题

右子树的根结点编号是多少?

{{ input(9) }}


第 10 题

在结点值互不相同的前提下,前序和中序能否唯一确定二叉树?

{{ select(10) }}

  • 不能

第 11 题

只给出前序和后序,通常能否唯一确定一般二叉树?

{{ select(11) }}

  • 不能

第 12 题

递归遍历代码把输出语句放在左右递归调用之间,得到的是( )。

{{ select(12) }}

  • 前序
  • 中序
  • 后序
  • 层序