Construct String from Binary Tree LeetCode solution in Python
1class Solution {2void tree2str(TreeNode* root,string &s) {3if(!root) return;45s+=to_string(root->val);67if(!root->left && !root->right) return;89s.push_back('('); tree2str(root->left,s); s.push_back(')');1011if(root->right){12s.push_back('('); tree2str(root->right,s); s.push_back(')');13}14}15public:16string tree2str(TreeNode* root) {17string ans = "";18tree2str(root,ans);19return ans;20}21};