diff options
Diffstat (limited to 'other/construct_string_binary_tree.cpp')
-rw-r--r-- | other/construct_string_binary_tree.cpp | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/other/construct_string_binary_tree.cpp b/other/construct_string_binary_tree.cpp new file mode 100644 index 0000000..a63e3c9 --- /dev/null +++ b/other/construct_string_binary_tree.cpp @@ -0,0 +1,21 @@ +#include "leetcode.h" + +class Solution { +public: + string tree2str(TreeNode *root) { + if (!root) + return ""; + string ans = to_string(root->val); + if (root->left || root->right) { + ans.push_back('('); + ans += tree2str(root->left); + ans.push_back(')'); + } + if (root->right) { + ans.push_back('('); + ans += tree2str(root->right); + ans.push_back(')'); + } + return ans; + } +}; |