A layout engine renders fastest when its split tree is height-balanced: at every node, the heights of the left and right subtrees differ by at most 1. The height of an empty subtree is 0.
Write isBalanced(root) that returns true if every node meets that rule and false otherwise. An empty tree is balanced.
Input: root = [1,2,3,4,5] Output: true
At the root, the heights are 2 and 1. Every other node is balanced too.
Input: root = [1,2,null,3] Output: false
The root's left subtree has height 2 and its right subtree height 0.
0 <= number of nodes <= 5000Trees are written level by level, like [1, 2, 3, null, 4], where null is a missing child. Your function works with TreeNode objects, each with val, left, and right.
Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #346 Balanced Layout Check. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.