首页 > 试题广场 >

(笛卡尔树)对于一个给定的两两不等的正整数序列,笛卡尔树是这

[填空题]
(笛卡尔树)对于一个给定的两两不等的正整数序列,笛卡尔树是这样的一棵二叉树:
首先,它是一个最小堆,即除了根结点外,每个结点的权值都大于父节点的权值;其次,它
的中序遍历恰好就是给定的序列。例如,对于序列 7、2、12、1、10、5、15、3,下图就
是一棵对应的笛卡尔树。现输入序列的规模 n(1≤n<100)和序列的 n 个元素,试求其对
应的笛卡尔树的深度 d(根节点深度为 1),以及有多少个叶节点的深度为 d。

const
  SIZE = 100;
  INFINITY = 1000000;
var
  n, maxDeep, num, i : integer;
  a : array[1..SIZE] of integer;
procedure solve(left, right, deep : integer);
var
  i, j, min : integer;
begin
  if deep > maxDeep then
  begin
    maxDeep := deep;
    num := 1;
  end
  else if deep = maxDeep then
    1;
  min := INFINITY;
  for i := left to right do
    if min > a[i] then
    begin
      min := a[i];
      2;
    end;
  if left < j then
    3;
  if j < right then
    4;
end;
begin
  readln(n);
  for i := 1 to n do
    read(a[i]);
  maxDeep := 0;
  solve(1, n, 1);
  writeln(maxDeep, ' ', num);
end.

这道题你会答吗?花几分钟告诉大家答案吧!