Submission

Status:
[PPPPPPPPPP]

Score: 100

User: njoop

Problemset: composite tree

Language: cpp

Time: 0.003 second

Submitted On: 2025-04-04 02:26:56

#include<bits/stdc++.h>
using namespace std;
struct node{
	int data;
	struct node* left;
	struct node* right;
};

struct node* newnode(int data){
	struct node* node = new struct node;
	node->data = data;
	node->left = NULL;
	node->right = NULL;
	return node;
}

struct node* makeTree(string str, int start, int end){
	if(start>end) return NULL;
	char now = str[start];
	if(now-'0'>0 && now-'0'<10){
		struct node* tree = newnode(now-'0');
		int s = start+1, e = end-1;
		int i = s;
		int count=0;
		do{
			if(str[i] == '(') count++;
			else if(str[i] == ')') count--;
			i++;
		}while(count);
		tree->data = now-'0';
		tree->left = makeTree(str,s+1,i-1);
		tree->right = makeTree(str,i+1,e);
		return tree;
	}else return NULL;
	
}

void preorder(struct node *p1, struct node* p2){
	cout<<p1->data+p2->data;
	cout<<'(';
	if(p1->left != NULL && p2->left != NULL) preorder(p1->left, p2->left);
	cout<<")(";
	if(p1->right != NULL && p2->right != NULL) preorder(p1->right, p2->right);
	cout<<')';
}
main(){
	string a, b;
	cin>>a>>b;
//	if(a == "5(2(8)())(1(6(2()())(3()()))())") a = "5(2(8()())())(1(6(2()())(3()()))())";
	struct node* treeA = makeTree(a,0,a.size());
	struct node* treeB = makeTree(b,0,b.size());
	preorder(treeA, treeB); cout<<endl;
}
/*
5(2(8)())(1(6(2()())(3()()))())
3(4()())(8(1(2()())())(3()()))
*/