已知前序后序求中序的一种 function ok(s1 s2:string):boolean;var i l:integer; p:boolean;

行业问题 面试题库 已帮助 人

已知前序后序求中序的一种

function ok(s1,s2:string):boolean;

var i,l:integer; p:boolean;

热搜:前序遍历序列与中序遍历序列相同的二叉树为

答案解析

难度★★☆ 搜索
  • 正确答案:

    begin
    ok:=true;
    l:=length(s1);
    for i:=1 to l do begin
    p:=false;
    for j:=1 to l do
    if s1[i]=s2[j] then p:=true;
    if not p then begin ok:=false;exit;end;
    end;
    end;

    procedure solve(pre,post:string);
    var i:integer;
    begin
    if (pre='') or (post='') then exit;
    i:=0;
    repeat
    inc(i);
    until ok(copy(pre,2,i),copy(post,1,i));
    solve(copy(pre,2,i),copy(post,1,i));
    midstr:=midstr+pre[1];
    solve(copy(pre,i+2,length(pre)-i-1),copy(post,i+1,length(post)-i-1));
    end;