Height-Balanced Binary Tree
Updated
Problem statement
You are given the root of a binary tree. Return true if the tree is height-balanced, and false otherwise.
The height of a subtree is the number of nodes on its longest path from its top node down to a leaf. An empty subtree has height 0.
A tree is height-balanced when, for every node, the heights of its left and right subtrees differ by at most 1. Checking only the root is not enough. An empty tree is height-balanced.
The node values do not affect the answer.
Function
isBalanced(root: TreeNode) → booleanExamples
Example 1
root = [1,2,3,4,5,null,6]return = trueNode 2 has two leaf children, so both of its subtrees have height 1. Node 3 has only a right child, so its subtree heights are 0 and 1. At the root, both subtrees have height 2. Every node passes, so the answer is true.
Example 2
root = [1,2,3,4,null,5,6,7,null,8]return = falseAt the root, both subtrees have height 3, so the root itself is fine. But node 2 has a left subtree of height 2 (nodes 4 and 7) and no right child, so its subtree heights are 2 and 0, which differ by 2. The answer is false.
Example 3
root = []return = trueThe tree is empty. An empty tree has no node that can break the rule, so it is balanced. The answer is true.
Constraints
- The tree has between
0and500nodes. -10^4 <= Node.val <= 10^4
More Expedia Onsite Interview questions
- Scramble StringONSITE INTERVIEW · Seen Sep 2026
- Smallest Sufficient TeamONSITE INTERVIEW · Seen Sep 2026
- Check Anagram Pairs Index by IndexONSITE INTERVIEW · Seen Oct 2025
- Fresh Oranges Left After Rotting StopsONSITE INTERVIEW · Seen Oct 2025
- Longest Increasing SubsequenceONSITE INTERVIEW · Seen Oct 2025
- Lowest Common Ancestor with Repeated and Missing ValuesONSITE INTERVIEW · Seen Oct 2025
- Shortest Substring with Exactly K OnesONSITE INTERVIEW · Seen Oct 2025
- Middle Element of a Linked ListONSITE INTERVIEW · Seen Nov 2024
- Minimum in a Rotated Sorted ArrayONSITE INTERVIEW · Seen Sep 2024
- Mirror Image Binary TreesONSITE INTERVIEW · Seen Sep 2024
Similar questions at other companies
- Binary Tree Level Order TraversalGoogle · ONSITE INTERVIEWGoogle interview questions
- Lowest Common Ancestor Only When Both Targets ExistAdobe · ONSITE INTERVIEWAdobe interview questions
- Sum Root-to-Leaf NumbersMeta · ONSITE INTERVIEWMeta interview questions
- Binary Tree Maximum Path SumGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
- Balanced Tree Node ReportNetflix · ONSITE INTERVIEWNetflix interview questions
- Trim a Binary Search TreeBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
- Maximum Value in a Binary TreeAkuna Capital · PHONE SCREENAkuna Capital interview questions
- Binary Tree Target-Sum PathsByteDance · PHONE SCREENByteDance interview questions
- Count Uni-Valued SubtreesAmazon · PHONE SCREEN
- Validate Binary Search TreeLyft · PHONE SCREENLyft interview questions
Interview reports that mention this question
Expedia interview experiences
- Expedia Intern Final Round in 2021: 28 Experiences (Part 4 of 6)
- Expedia Intern Finals Before 2020: 14 Experiences (Part 6 of 6)
- Expedia Intern Final Round in 2020: 24 Experiences (Part 5 of 6)
- Expedia Intern Final Round in 2022: 15 Experiences (Part 3 of 6)
- Expedia SWE Intern Final Round: 126 Experiences (Part 1 of 6)