FastPrepCheck Whether All Leaves Are at the Same Level
Problems/Check Whether All Leaves Are at the Same Level

Check Whether All Leaves Are at the Same Level

Amazon logoAmazon● EasyFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Amazon Experience 14 links to the well-known task of checking whether all leaf nodes are at the same level. It was an onsite interview. The report names the problem but gives no function signature or limits. The binary tree input details below are the usual ones. Our version is about 98% the same as the reported question.

You are given the root of a nonempty binary tree. Return true if every leaf is at the same depth, and false otherwise.

A leaf is a node with no left or right child. The root has depth 0, and each step down adds one. Only whether the leaf depths are equal matters.

Function

leavesAtSameLevel(root: TreeNode) → boolean

Examples

Example 1

root = [1,2,3,4,5,6,7]return = true
root
1245367

All four leaves sit two edges below the root. Their depths are equal, so the answer is true.

Example 2

root = [1,2,3,4,null,null,null,8]return = false
root
12483

Node 3 is a leaf at depth one, while node 8 is a leaf at depth three. The leaf depths differ, so the answer is false.

Example 3

root = [1,2,3,null,4,5,null]return = true
root
12435

Leaves 4 and 5 are both at depth two, even though one is a right child and the other is a left child. The answer is true.

Constraints

  • The tree contains between 1 and 100000 nodes.
  • -10^9 <= node.val <= 10^9.

More Amazon Onsite Interview questions

  • LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Maximal SquareONSITE INTERVIEW · Seen Nov 2026
  • Serialize and Deserialize a Binary TreeONSITE INTERVIEW · Seen Nov 2026
  • Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Course Schedule IIPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Diagonal Traversal of a Binary TreeONSITE INTERVIEW · Seen Nov 2026
  • Minimum Time to Spread Through a GridPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Optimal Account BalancingONSITE INTERVIEW · Seen Nov 2026
  • Task SchedulerOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Number of IslandsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026

Similar questions at other companies

  • Maximum Depth of Binary TreeLinkedIn · PHONE SCREEN · ONSITE INTERVIEWLinkedIn interview questions
  • Binary Tree Level-Order Traversal by LevelsAdobe · PHONE SCREEN · ONSITE INTERVIEWAdobe interview questions
  • Symmetric Binary TreeFigma · ONSITE INTERVIEWFigma interview questions
  • Organization Chart by LevelMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
  • Tree Level with the Most NodesBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
  • Mirror Image Binary TreesExpedia · ONSITE INTERVIEWExpedia interview questions
  • Mirror a Binary TreeAMD · ONSITE INTERVIEWAMD interview questions
  • Find a Node’s Level in an N-ary TreeMorgan Stanley · ONSITE INTERVIEWMorgan Stanley interview questions
  • Symmetric Binary TreeCapital One · ONSITE INTERVIEWCapital One interview questions
  • Delete Nodes and Return Forest RootsGoogle · ONSITE INTERVIEWGoogle interview questions

Amazon interview experiences

  • Amazon Experienced SDE Onsite Questions: 1 Experience
  • Amazon SDE Onsite Interview Questions: 9 Experiences
  • Amazon SDE II and L5 FDE Interview Questions: 4 Experiences
  • Amazon Full-Time OA Problems, Earlier Reports (Part 5 of 5)
  • Amazon Full-Time OA Problems, Recent Reports (Part 4 of 5)
  • Amazon New Grad OA Problems: Every Task We Have (Part 3 of 5)
See Amazon hiring insights
Saved
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public boolean leavesAtSameLevel(TreeNode root) {
      // write your code here
    }
}
Checking account…