All problemsBack
Balanced Binary Tree
easyGiven a binary tree, determine if it is height-balanced — that is, a tree in which the left and right subtrees of every node differ in height by no more than 1.
Test Cases
Copy an input into themain harness and Run to verifyInput
root = [3,9,20,null,null,15,7]Expected Output
trueInput
root = [1,2,2,3,3,null,null,4,4]Expected Output
falseInput
root = []Expected Output
trueConstraints
- The number of nodes in the tree is in the range [0, 5000].
- -10^4 <= Node.val <= 10^4
Hints
Hint 1 — click to reveal
Computing the height separately at every node repeats a lot of work.
Hint 2 — click to reveal
Let one traversal return the height *and* signal imbalance — a sentinel like -1 works well.
Java Compiler
Powered by OneCompiler. Starter code loads automatically — edit and hit Run.