基本思想:
要求用两个序列构建新的二叉树,标准写法,注意下;
关键点:
无;
#include<stdio.h> #include<stdlib.h> #include<iostream> #include<string> #include<vector> #include<algorithm> #include<map> #include<set> using namespace std; string s1, s2; struct node { char data; node* left; node* right; }; node* charge(int l1, int r1, int l2, int r2) { if (l1 > r1) return NULL; int index = -1; for (int i = l2; i <= r2; i++) { if (s2[i] == s1[l1]) index = i; } node* no = new node; no->data = s1[l1]; no->left = charge(l1+1,l1+index-l2,l2,index-1); no->right = charge(l1+index-l2+1,r1,index+1,r2); return no; } void postorder(node* root) { if (root == NULL) return; postorder(root->left); postorder(root->right); cout << root->data; } int main() { while (cin >> s1>> s2) { node* root = charge(0, s1.size() - 1, 0, s2.size() - 1); postorder(root); cout << endl; } return 0; }
原文地址:https://www.cnblogs.com/songlinxuan/p/12416985.html
时间: 2024-10-06 14:05:53