Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Binary Tree Height

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Binary Tree Height. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Mavericks Consulting's hierarchy analysis tool represents each organizational structure as a binary tree. Given the tree's root, return its height, measured as the number of nodes on the longest path from the root to any leaf.

Use an iterative breadth-first search. Process one complete level at a time and count how many levels the traversal contains.

Formal Specification

The input root is either None or a nested dictionary with this structure:

  • value: an integer node value
  • left: another node dictionary or None
  • right: another node dictionary or None

Return an integer representing the tree height. An empty tree has height 0, and a tree containing only its root has height 1.

Constraints

  • The tree contains between 0 and 10^5 nodes.
  • Each node value is an integer between -10^9 and 10^9.
  • Each node has at most one left child and one right child.
  • The input is a valid binary tree.

Function Signature

def tree_height(root):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output