/**
* Definition for a binary tree node.
* struct TreeNode {
* string val;
* TreeNode *left;
* TreeNode *right;
* };
*/
class Solution {
public:
void dfs (TreeNode *root , string &res)
{
if(!root) return;
if(root->left == NULL && root->right == NULL)
{
res += root -> val;
}
else
{
res += '(';
dfs(root->left ,res );
res += root->val;
dfs(root->right , res);
res += ')';
}
}
string expressionTree(TreeNode* root) {
string res = "";
dfs(root->left , res);
res += root->val;
dfs(root->right ,res);
return res;
}
};
根节点另外拿出来