DSA Section

AVL Tree

Detailed guide and Python implementation for the 'AVL Tree' problem.

問題提起

Write a function is_avl_balanced(tree_arr) that takes an array representation of a binary tree tree_arr and returns True if the tree is height-balanced (for every node, the height of its left and right subtrees differs by at most 1) and is a valid BST, or False otherwise.

制約
  • 0 <= len(tree_arr) <= 1000

Example 1
Input
tree_arr = [3, 9, 20, None, None, 15, 7]
Output
True
Explanation

The tree is a valid BST and the depth difference of left/right subtrees of all nodes is at most 1.

Example 2
Input
tree_arr = [1, 2, None, 3, None, None, None, 4]
Output
False
Explanation

The tree is unbalanced because leaf node 4 is at depth 4 while right subtree of node 1 is empty.

Need a Hint?
Consider using Trees-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

解決する準備はできましたか?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

エディタで開く
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

推奨される Python リソース

関連するインタラクティブなチュートリアル、チートシート、コード比較で知識を深めてください。