二叉树,前序+中序=>后序

    xiaoxiao2021-03-25  104

    #include<iostream> #include<cstdio> #include<vector> #include<cstring> using namespace std; const int maxn = 1000+10; char p[maxn], m[maxn], b[maxn]; int n; struct node { struct node *l, *r; char c; node(char _c) { c = _c; this->l = NULL; this->r = NULL; } }; node* bit(char *p, char *m, int len) { // 根结点,将二叉树一分为二 int pos=-1; if (len == 0) return NULL; for (int i=0; i<len; i++) { if (p[0] == m[i]) { //查找根结点 pos = i; break; } } node *root = new node(p[0]); root->l = bit(p+1, m, pos); root->r = bit(p+pos+1, m+pos+1, len-pos-1); return root; } void post(node *root) { if (root == NULL) return ; post(root->l); post(root->r); printf("%c", root->c); } int main() { while (scanf("%s%s", p, m)) { n = strlen(p); node *root =NULL; root = bit(p, m, n); post(root); printf("\n"); } return 0; }
    转载请注明原文地址: https://ju.6miu.com/read-14219.html

    最新回复(0)