11

Gooooogle  •  4个月前


#include <iostream>
#include <string>

using namespace std;
int sum = 0;
int	i = 1;
typedef struct TreeNode {
	char data;
	TreeNode *left, *right;
} BiTNode, *BiTree;

void CreateTree(BiTree &root, string s, string t) {
	if (s.size() == 0) {
		root = NULL;
		return ;
	}
	char root_node = s[0];
	int pos = t.find(root_node);
	string lt = t.substr(0, pos);
	string rt = t.substr(pos + 1);
	int llen = lt.size();
	int rlen = rt.size();
	string ls = s.substr(1, llen);
	string rs = s.substr(llen + 1);
	root = new TreeNode;
	if (root) {
		root->data = root_node;
		CreateTree(root->left, ls, lt);
		CreateTree(root->right, rs, rt);
	}
}

void Print(BiTree root) {
	if (root) {
		Print(root -> left);
		Print(root -> right);
		cout << root->data;

	}
}

int main() {
	string s, t;
	BiTree root;
	cin >> s >> t;
	CreateTree(root, s, t);
	Print(root);
	return 0;

评论:

typedef struct TreeNode {

char data;
TreeNode *left, *right;

} BiTNode, *BiTree; 就这里


♻️lzhh_lzhh32  •  3个月前

请先登录,才能进行评论