Construct String from Binary Tree LeetCode solution in C++
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('(');10tree2str(root->left, s);11s.push_back(')');1213if (root->right) {14s.push_back('(');15tree2str(root->right, s);16s.push_back(')');17}18}1920public:21string tree2str(TreeNode *root) {22string ans = "";23tree2str(root, ans);24return ans;25}26};