Leetcode 606: Construct String from Binary Tree
Given the root node of a binary tree, generate a string representation of the tree following specific formatting rules based on a preorder traversal.
Problem
Approach
Steps
Complexity
Input: The input is the root node of a binary tree, where each node is represented by its integer value. The tree can have any configuration of left and right children.
Example: Input: root = [1, 2, 3, null, 4]
Constraints:
• The binary tree has at least one node.
• Node values are integers in the range [-1000, 1000].
Output: Return a string representing the binary tree following a preorder traversal, with proper parentheses indicating child nodes.
Example: Output: '1(2()(4))(3)'
Constraints:
• The output is a string representation of the tree.
Goal: To convert a binary tree into a string format by recursively visiting each node in preorder and formatting it with parentheses for children.
Steps:
• Perform a preorder traversal of the binary tree.
• For each node, if it has a left child, include it in parentheses.
• If the node has a right child but no left child, include empty parentheses '()' before the right child.
• Concatenate the node's value and the formatted strings of its children.
Goal: The binary tree has a root node, and each node has an integer value. The tree will have at least one node.
Steps:
• The number of nodes in the tree is between 1 and 10,000.
• Node values are integers between -1000 and 1000.
Assumptions:
• The tree is a valid binary tree with a single root node.
• Input: Input: root = [1, 2, 3, null, 4]
• Explanation: In this case, node 1 has two children, node 2 and node 3. Node 2 itself has a right child (node 4), and node 3 has no children. The string representation is '1(2()(4))(3)' to reflect the structure.
Approach: The solution involves recursively traversing the binary tree in preorder and building the string representation based on the specified format.
Observations:
• Each node needs to be represented by its value, followed by parentheses enclosing its children (if any).
• Start with the root and recursively build the string for its left and right children. Ensure that empty parentheses are only added when necessary.
Steps:
• Define a recursive function to handle the string generation.
• Check for the presence of left and right children for each node.
• Ensure that empty parentheses are omitted except for when a node has a right child but no left child.
Empty Inputs:
• The input will always contain at least one node, so empty inputs are not an issue.
Large Inputs:
• The solution should handle up to 10,000 nodes efficiently.
Special Values:
• Ensure proper handling of trees with only left children, only right children, or no children at all.
Constraints:
• All nodes in the binary tree are valid integers within the specified range.
string tree2str(TreeNode* root) {
if(root == NULL) return "";
string left = tree2str(root->left);
if(left!= "") left = '(' + left + ')';
string right = tree2str(root->right);
if(right != "") right = '(' + right + ')';
if(left == "" && right != "") left = "()";
// if(left != "" && right == "") right = "()";
return to_string(root->val)+left+right;
}
1 : Function Declaration
string tree2str(TreeNode* root) {
Defines the function `tree2str` which takes a pointer to a TreeNode as input and returns a string representing the tree structure.
2 : Base Case
if(root == NULL) return "";
Handles the base case of the recursion: if the current node is NULL, it returns an empty string.
3 : Recursive Call (Left Subtree)
string left = tree2str(root->left);
Recursively calls `tree2str` on the left child of the current node to get the string representation of the left subtree.
4 : Left Subtree Formatting
if(left!= "") left = '(' + left + ')';
If the left subtree is not empty, formats it by surrounding the result with parentheses.
5 : Recursive Call (Right Subtree)
string right = tree2str(root->right);
Recursively calls `tree2str` on the right child of the current node to get the string representation of the right subtree.
6 : Right Subtree Formatting
if(right != "") right = '(' + right + ')';
If the right subtree is not empty, formats it by surrounding the result with parentheses.
7 : Handle Empty Left Subtree
if(left == "" && right != "") left = "()";
If the left subtree is empty but the right subtree is not, adds `()` to represent the missing left child.
8 : Return Statement
return to_string(root->val)+left+right;
Constructs the final string by concatenating the root's value with the string representations of the left and right subtrees.
Best Case: O(n)
Average Case: O(n)
Worst Case: O(n)
Description: The time complexity is O(n), where n is the number of nodes in the binary tree, since we perform a preorder traversal of all nodes.
Best Case: O(n)
Worst Case: O(n)
Description: The space complexity is O(n) because we store the string representation of the tree, which can be proportional to the number of nodes in the tree.
LeetCode Solutions Library / DSA Sheets / Course Catalog |
---|
comments powered by Disqus