Grokking Tree Coding Patterns for Interviews
Ask Author
Back to course home

0% completed

Solution: Check if all leaves are at same level
Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Problem Statement

Given a root of the binary tree, return true if all the leaf nodes of this tree exist at the same depth. Otherwise, return false.

Examples

Example 1:

  • Input: root = [1, 2, 3, 4, null, null, 5]
  • Expected Output: true
  • Justification: The leaves are nodes 4 and 5, and they are both at the same level (level 3).

Example 2:

  • Input: root = [12, 20, 13, null, 10, 11, null, 16]
  • Expected Output: false
  • Justification: The leaf node 11 is at depth 3 and leaf node 16 is at depth 4

.....

.....

.....

Like the course? Get enrolled and start learning!

Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible