列車の配置再帰先行順にアルゴリズムを探しています

トピックリンク

思考

順序シーケンスループでは再び、実際には、帰りがけ順の最後のノードをルートノードを見つけます。このようなトラバーサル順序は、左と右のサブツリーを繰り返す内で操作する前に、左と右のサブツリーに分割されます。

コード

#include <bits/stdc++.h>
using namespace std;
string in,post;
void preorder(int root,int st,int ed){
    if(st>ed) return;
    int i;
    for(i=st;i<=ed;i++) if(in[i]==post[root]) break;
    cout<<post[root];
    preorder(root-1-ed+i,st,i-1);//左子树
    preorder(root-1,i+1,ed);//右子树
}
int main(){
    cin>>in>>post;
    preorder((int)in.length()-1,0,(int)in.length()-1);
    return 0;
}

公開された83元の記事 ウォンの賞賛9 ビュー10000 +

おすすめ

転載: blog.csdn.net/weixin_43077261/article/details/103737141