AcWing 3765. 表达式树
原题链接
简单
作者:
叫我去学习_7
,
2021-07-19 15:10:12
,
所有人可见
,
阅读 267
/**
* Definition for a binary tree node.
* struct TreeNode {
* string val;
* TreeNode *left;
* TreeNode *right;
* };
*/
class Solution {
public:
string res;
void dfs(TreeNode *root){
if(!root) return;
if(!root->right && !root->left){
res += root->val;
}else{
res += '(';
dfs(root->left);
res += root->val;
dfs(root->right);
res += ')';
}
}
string expressionTree(TreeNode* root) {
dfs(root->left); res += root->val; dfs(root->right);
return res;
}
};