Before trusting a saved index, a client checks that it is a valid AVL tree. That means two things at once: it is a binary search tree (every left descendant strictly smaller and every right descendant strictly bigger than each node), and at every node the heights of the left and right subtrees differ by at most 1.
Write isAvlIndex(root) that returns true if both rules hold everywhere, and false otherwise. An empty tree is valid.
Input: root = [20,10,30,5,15] Output: true
It is sorted, and every node's two sides differ in height by at most one.
Input: root = [1,null,2,null,3] Output: false
Sorted, but the root's right side has height 2 and its left side 0.
0 <= number of nodes <= 10^4-10^9 <= value <= 10^9Trees 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 #375 Is the Index Height-Balanced and Sorted?. 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.