Submission

Status:

[PPPPPPPPPP]

Subtask/Task Score:

{100/100}

Score: 100

User: mydKN

Problemset: composite tree

Language: cpp

Time: 0.002 second

Submitted On: 2026-03-09 22:34:49

#include <bits/stdc++.h>

using namespace std;

struct node{
	int val;
	node* left = NULL;
	node* right = NULL;
};

node* build(string& str, int& idx){
	if(idx >= str.length()) return NULL;
	if(str[idx] == ')') return NULL;
	node* tree = new node;
	tree->val = str[idx] - '0';
	++idx;
	if(idx < str.length() && str[idx] == '('){
		++idx;
		tree->left = build(str, idx);
		if(idx < str.length() && str[idx] == ')') ++idx;
	}
	if(idx < str.length() && str[idx] == '('){
		++idx;
		tree->right = build(str, idx);
		if(idx < str.length() && str[idx] == ')') ++idx;
	}
	return tree;
}

void out(node* t1, node* t2){
	cout << t1->val + t2->val;
	cout << "(";
	if(t1->left != NULL && t2->left != NULL) out(t1->left, t2->left);
	cout << ")(";
	if(t1->right != NULL && t2->right != NULL) out(t1->right, t2->right);
	cout << ")";
}

int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);
	string s1, s2;
	cin >> s1 >> s2;
	int idx = 0;
	node* t1 = build(s1, idx);
	idx = 0;
	node* t2 = build(s2, idx);
	out(t1, t2);
}