MEDIUM NC#49 Trees
110. Balanced Binary Tree
📖 Problem
Given a binary tree, determine if it is height-balanced. A height-balanced binary tree is a binary tree in which the left and right subtrees of every node differ in height by no more than 1.
🧠 Visual Learning Aid
1 Model the input into the right structure
2 Choose the core technique and invariant
3 Execute step-by-step with a sample
4 Validate complexity and edge cases
JS/TS Refreshers
- •Array methods (`push`, `pop`, `shift`, `slice`)
- •Object/Map/Set usage patterns
- •Function parameter and return typing
- •In-place array updates
- •Sorted array traversal
- •Boundary condition checks
Logical Thinking Concepts
- •Define invariants before coding
- •Check edge cases first (`[]`, single element, duplicates)
- •Estimate time/space before implementation
- •Apply Two Pointers reasoning pattern
- •Apply BFS reasoning pattern
- •Apply Recursion reasoning pattern
💡 Approach
- → Calculate height for each node recursively
- → If subtree is unbalanced (height diff > 1), return -1
- → Tree is balanced if root height is not -1
- → Time: O(n), Space: O(h) where h is tree height
🧭 Prerequisites
🛠️ Hints & Pitfalls
Hints
- •Calculate height for each node recursively
- •If subtree is unbalanced (height diff > 1), return -1
- •Tree is balanced if root height is not -1
Common Pitfalls
- •Time: O(n), Space: O(h) where h is tree height
🧪 Test Cases
Hidden tests on submit: 1
Test Case 1
Not run Input:
checkHeight(node: TreeNode | null); Expected:
Computed from hidden reference Test Case 2
Not run Input:
checkHeight(node.left); Expected:
Computed from hidden reference Test Case 3
Not run Input:
checkHeight(node.right); Expected:
Computed from hidden reference 📝 Code Editor
📤 Output