Depth of Binary Tree
Medium Tree String Depth-First Search
🏢LinkedIn
0 views0 likesProblem Description
Depth of Binary Tree
You are given a binary tree in a peculiar string representation. Each node is written in the form (lr), where l corresponds to the left child and r corresponds to the right child. If either l or r is null, it will be represented as a zero. Otherwise, it will be represented by a new (lr) pair.
Write a function that takes this string representation of a binary tree and returns the depth of the tree.
Example 1:
Input: (00)
Output: 1Example 2:
Input: ((00)(00))
Output: 2Example 3:
Input: ((((00)0)0)0)
Output: 4Constraints:
- The string representation will be a valid binary tree format.
- The length of the string will not exceed 10^4 characters.
Hints
Use a stack to simulate the depth of the tree.
Track the maximum depth encountered during the traversal of the string.