Balance
Time limit1sMemory limit1024 MB
Given a vertex budget V, find the maximum possible height of an AVL tree built with at most V vertices.
- Level
Medium4 of 10
- Topics
- Dynamic programming, Tree, Binary search, Math
- Solved
- No attempts yet
Problem
AVL Tree, a kind of binary search tree, keeps itself balanced using a property called the height balance property.
Every binary search tree that satisfies the height balance property is an AVL Tree.
For every internal vertex of a tree , if the heights of the left subtree and right subtree of differ by at most 1, then is said to satisfy the height balance property.

In the figure above, the tree on the left is an AVL Tree because every internal vertex has a left subtree and right subtree of equal height.
The tree in the middle is an AVL Tree because vertices 5, 6, and 8 have left and right subtrees whose heights differ by 1, and all other vertices have a difference of 0.
The tree on the right is not an AVL Tree because vertex 8 has left and right subtrees whose heights differ by 2.
Given a positive integer , write a program that prints the maximum height of an AVL Tree that can be built using at most vertices.
Input
The first line gives the number of test cases .
From the second line, lines follow, each giving the number of vertices .
Output
Print the answer over lines.
For each test case, print the maximum height of an AVL Tree that can be built with at most vertices.