等价终极表达式
#include <iostream>
#include <string>
using namespace std;
string str;
int i;
int j=0;
int cnt = 0;
struct TreeNode
{
char value;
struct TreeNode* lchild, * rchild;
TreeNode(char c) : value(c), lchild(NULL), rchild(NULL) {}
};
TreeNode* creatTree()
{
char c = str[i++];
if (c == '#') return NULL;
TreeNode* root = new TreeNode(c);
root->lchild = creatTree();
root->rchild = creatTree();
return root;
}
void inorder(TreeNode* root)
{
if (!root)
{
return;
}
if(root->lchild==NULL&&root->rchild==NULL)
cout << root->value;
else
{
if (cnt != 0)
{
cout << "(";
}
cnt++;
inorder(root->lchild);
cout << root->value;
inorder(root->rchild);
if (cnt-1 != j)
{
cout << ")";
j++;
}
}
}
int main()
{
cin >> str;
i = 0;
TreeNode* root = creatTree();
inorder(root);
cout << endl;
return 0;
}