↓Skip to main content
  1. LeetCode/

LeetCode 606: Construct String from Binary Tree

·1 min· ·
LeetCode Medium String Tree Depth-First-Search Binary-Tree
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2022-08-28

  • Submissions: Wrong Answer → Accepted ×4

Approach
#

Pre-order: the value, then the subtrees (Tree Template 1). Parenthesis rules:

  • Leaf: just the value.
  • Empty right subtree: omit its parentheses.
  • Empty left but non-empty right: keep an empty () on the left, or the right child would look like a left child.

My Attempts
#

Attempt 1 returned a list and wrapped the root in parentheses too: [1] returned ['(', '1', ')'] instead of "1".

Solution
#

class Solution:
    def tree2str(self, root: Optional[TreeNode]) -> str:
        if not root:
            return ""
        s = str(root.val)
        if root.left or root.right:
            s += f"({self.tree2str(root.left)})"
        if root.right:
            s += f"({self.tree2str(root.right)})"
        return s

Complexity
#

Time O(n²) from string concatenation; collecting into a list and joining gives O(n).

Mistakes to Avoid
#

  • () is needed only when the left subtree alone is missing.
  • The root itself has no parentheses.

Interview Explanation
#

A pre-order recursion writes the value, then the left subtree in parentheses whenever any child exists (an empty one as ()), then the right subtree only if it exists. Time O(n) to O(n²).

Related

LeetCode 235: Lowest Common Ancestor of a Binary Search Tree
·2 mins
LeetCode Medium Tree Depth-First-Search Binary-Search-Tree Binary-Tree
LeetCode 235 note: go left when both p and q are smaller, right when both are larger; otherwise (split, or one is the node itself) this node is the LCA
LeetCode 563: Binary Tree Tilt
·1 min
LeetCode Easy Tree Depth-First-Search Binary-Tree Dp-on-Trees
LeetCode 563 note: return subtree sums in post-order; each node’s tilt is the difference of its subtree sums, added to the answer
LeetCode 572: Subtree of Another Tree
·2 mins
LeetCode Easy Tree Depth-First-Search String-Matching Binary-Tree
LeetCode 572 note: for every node of root, check whether its subtree equals subRoot; recurse with isSubtree, not by comparing only the children
LeetCode 501: Find Mode in Binary Search Tree
·2 mins
LeetCode Easy Tree Depth-First-Search Binary-Search-Tree Binary-Tree
LeetCode 501 note: a BST’s in-order is sorted, so equal values are contiguous; count the current run during in-order, resetting the answer on a new maximum and appending on a tie
LeetCode 543: Diameter of Binary Tree
·2 mins
LeetCode Easy Tree Depth-First-Search Binary-Tree Dp-on-Trees
LeetCode 543 note: the diameter may skip the root; return heights in post-order while updating the answer with left + right height at every node, counting edges
LeetCode 404: Sum of Left Leaves
·1 min
LeetCode Easy Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 404 note: tell each child during DFS whether it is a left child, adding only left children that are leaves