Skip to main content

Depth of Binary Tree

Medium
LinkedIn
0 views
0 likes
Tree String Depth-First Search

Problem 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: 1

Example 2:

Input: ((00)(00))
Output: 2

Example 3:

Input: ((((00)0)0)0)
Output: 4

Constraints:

  • The string representation will be a valid binary tree format.
  • The length of the string will not exceed 10^4 characters.

Solution

Click to load code editor