Tap here to switch tabs
Problem
Submissions

Problem: Construct Binary Tree from String

Try to solve the Construct Binary Tree from String problem.

Statement

Given a string s that represents a binary tree, construct and return the root of the binary tree.

The input string consists of an integer representing the root value, followed by zero, one, or two pairs of parentheses. Each pair of parentheses contains a child subtree structured in the same way. If a node has only one child, that child is always the left child.

Constraints:

  • 0≤0 \leq s.length ≤3imes104\leq 3 imes 10^4

  • s consists of digits, '(', ')', and '-' only.

  • All numbers in the tree have value at most 2302^{30}.

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Construct Binary Tree from String

Try to solve the Construct Binary Tree from String problem.

Statement

Given a string s that represents a binary tree, construct and return the root of the binary tree.

The input string consists of an integer representing the root value, followed by zero, one, or two pairs of parentheses. Each pair of parentheses contains a child subtree structured in the same way. If a node has only one child, that child is always the left child.

Constraints:

  • 0≤0 \leq s.length ≤3imes104\leq 3 imes 10^4

  • s consists of digits, '(', ')', and '-' only.

  • All numbers in the tree have value at most 2302^{30}.