文章目录
一、根据二叉树创建字符串
1、题目讲解
2、思路讲解
3、代码实现
class Solution {
public:
string tree2str(TreeNode* root)
{
string s;
if(root==nullptr)
return s;
s+=to_string(root->val);
if(root->left || (root->left==nullptr && root->right))
{
s+='(';
s+=tree2str(root->left);
s+=')';
}
if(root->right)
{
s+='(';
s+=tree2str(root->right);
s+=')';
}
return s;
}
};