FastPrepAll Tree Nodes at the Same Depth
Problems/All Tree Nodes at the Same Depth

All Tree Nodes at the Same Depth

AMD logoAMD● EasyFULLTIMEONSITE INTERVIEW
QuestionFunctionExamplesConstraints

Problem statement

Source note: The SDE2 report says to check whether all nodes in a tree are at the same level. It does not clarify whether the author meant all nodes or only leaves. This practice preserves the literal all-node reading; it does not claim to recover a leaf-depth task. Empty behavior, tree serialization, bounds and interface are authored. Approximate source match 60%.

Return whether every node in the binary tree has the same depth. Include the root, internal nodes and leaves. The root has depth 0; each child has its parent's depth plus 1. Return true for an empty tree. Node values do not affect the answer. Leave the tree unchanged.

This exercise uses the reported word nodes literally. A root with any child already contains nodes at different depths.

Function

allNodesSameDepth(root: TreeNode) → boolean

Examples

Example 1

root = [7]return = true

The only node is the root, at depth 0.

Example 2

root = [1,2,3]return = false

The root has depth 0 and both children have depth 1. Equal leaf depths do not make all node depths equal.

Constraints

  • 0 <= serialized tree length <= 400.
  • -1000 <= every non-null node value <= 1000.
  • The input is a valid connected acyclic binary tree serialized in level order, with null for missing children; [] denotes an empty tree.

More AMD problems

  • Insert After a Linked-List NodeONSITE INTERVIEW · Seen Oct 2026
  • LRU CacheONSITE INTERVIEW · Seen Oct 2026
  • Lowest Common Ancestor of a Binary TreePHONE SCREEN · Seen Oct 2026
All AMD interview questionsSee AMD hiring insights
Saved
public boolean allNodesSameDepth(TreeNode root) {
    // Write your code here.
}
Checking account…