Check Whether All Leaves Are at the Same Level
Updated
Problem statement
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) → booleanExamples
Example 1
root = [1,2,3,4,5,6,7]return = trueAll 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 = falseNode 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 = trueLeaves 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
1and100000nodes. -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)