Construct String from Binary Tree
Problem
Given the root
of a binary tree, construct a string consisting of parenthesis and integers from a binary tree with the preorder traversal way, and return it.
Omit all the empty parenthesis pairs that do not affect the one-to-one mapping relationship between the string and the original binary tree.
Constraints
- The number of nodes in the tree is in the range
[1, 104]
. -1000 <= Node.val <= 1000
Solution
The problem Construct String from Binary Tree
can be solved by recursively constructing string from child nodes based on the problem description.
Implementation
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution
{
public:
string tree2str(TreeNode *root)
{
if (root == NULL)
return "";
string ret = to_string(root->val);
if (root->left != NULL || root->right != NULL)
ret += "(" + tree2str(root->left) + ")";
if (root->right != NULL)
ret += "(" + tree2str(root->right) + ")";
return ret;
}
};