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

0% completed

Vote For New Content
Check if all leaves are at same level (medium)
On this page

Problem Statement

Examples

Try it yourself

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]
Image
  • 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]
Image
  • Expected Output: false
  • Justification: The leaf node 11 is at depth 3 and leaf node 16 is at depth 4. So, both leaf nodes are at different depth.

Example 3:

  • Input: root = [10, 8, 15, 17, null, 12]
Image
  • Expected Output: true
  • Justification: The leaves are nodes 17, and 12, and all are located at the same level (level 3).

Try it yourself

Try solving this question here:

Python3
Python3

. . . .

.....

.....

.....

Like the course? Get enrolled and start learning!

On this page

Problem Statement

Examples

Try it yourself