AVL Ağacı
'AVL Ağacı' sorunu için ayrıntılı kılavuz ve Python uygulaması.
1. Öğren
'AVL Ağacı' sorunu Ağaçlar bölümündeki önemli bir sorundur.
Bu uygulama Python'deki orta düzey mantığa odaklanır.
Sunduğumuz çözümlerde teknik doğruluğu ve kod okunabilirliğini ön planda tutuyoruz.
2. Real-World Applications
3. Visual Intuition
AVL Ağacı için mantık akışını görselleştirme.
4. Prerequisites
5. Step-by-Step Thinking
1. Understand the problem
AVL Ağacına ilişkin sorun bildirimini dikkatlice okuyun.
2. Formulate brute force
Basit bir yinelemeli çözüm taslağı oluşturun.
3. Identify inefficiency
Gereksiz hesaplamaları arayın.
4. Optimize search path
Süreci hızlandırmak için karma veya sıralama kullanın.
5. Final Implementation
Üretim standartları kodunu temizleyin.
Sorun Bildirimi
Bir tree_arr ikili ağacının dizi temsilini alan ve eğer ağaç yükseklik dengeliyse (her düğüm için sol ve sağ alt ağaçların yüksekliği en fazla 1 farklılık gösterir) ve geçerli bir BST ise True döndüren ve geçerli bir BST ise False döndüren bir is_avl_balanced(tree_arr) fonksiyonu yazın, aksi halde False.
- •0 <= len(tree_arr) <= 1000
Örnekler
tree_arr = [3, 9, 20, None, None, 15, 7]
True
The tree is a valid BST and the depth difference of left/right subtrees of all nodes is at most 1.
tree_arr = [1, 2, None, 3, None, None, None, 4]
False
The tree is unbalanced because leaf node 4 is at depth 4 while right subtree of node 1 is empty.
Need a Hint?
Edge Cases to Watch
- Boş giriş yapıları
- Tek eleman girişleri
- Büyük sayısal sınırlar
Çözmeye Hazır mısınız?
Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.
Mülakat Bilgileri ve Çeşitleri
Karmaşıklık Analizi Dökümü
Neden Zaman: Directly evaluates all possibilities.
Neden Uzay: Uses standard local memory.
Neden Zaman: Optimized paths reduce total operations.
Neden Uzay: May trade memory for speed.
Optimize Edilmiş Çözüm Python Kodu
Optimize Edilmiş Çözüm Python Kodu
def create_avl_tree_opt(arr: list) -> list:
class AVLTreeNode:
def __init__(self, val=0):
self.val = val
self.left = None
self.right = None
self.height = 1
def get_height(node):
return node.height if node else 0
def get_balance(node):
return get_height(node.left) - get_height(node.right) if node else 0
def rotate_right(y):
x = y.left
T2 = x.right
x.right = y
y.left = T2
y.height = 1 + max(get_height(y.left), get_height(y.right))
x.height = 1 + max(get_height(x.left), get_height(x.right))
return x
def rotate_left(x):
y = x.right
T2 = y.left
y.left = x
x.right = T2
x.height = 1 + max(get_height(x.left), get_height(x.right))
y.height = 1 + max(get_height(y.left), get_height(y.right))
return y
def insert(node, val):
if not node:
return AVLTreeNode(val)
if val < node.val:
node.left = insert(node.left, val)
else:
node.right = insert(node.right, val)
node.height = 1 + max(get_height(node.left), get_height(node.right))
balance = get_balance(node)
# Left Left
if balance > 1 and val < node.left.val:
return rotate_right(node)
# Right Right
if balance < -1 and val > node.right.val:
return rotate_left(node)
# Left Right
if balance > 1 and val > node.left.val:
node.left = rotate_left(node.left)
return rotate_right(node)
# Right Left
if balance < -1 and val < node.right.val:
node.right = rotate_right(node.right)
return rotate_left(node)
return node
if not arr: return []
root = None
for val in arr:
root = insert(root, val)
# Serialize level-order
res = []
queue = [root]
while queue:
curr = queue.pop(0)
if curr:
res.append(curr.val)
queue.append(curr.left)
queue.append(curr.right)
else:
res.append(None)
while res and res[-1] is None:
res.pop()
return resKaba Kuvvet Kodu (Spoiler Korumalı)
Kaba Kuvvet Kodu (Spoiler Korumalı)
def create_avl_tree_brute(arr: list) -> list:
# Standard AVL tree insertion with rotations, returns level order list
class AVLTreeNode:
def __init__(self, val=0):
self.val = val
self.left = None
self.right = None
self.height = 1
def get_height(node):
return node.height if node else 0
def get_balance(node):
return get_height(node.left) - get_height(node.right) if node else 0
def rotate_right(y):
x = y.left
T2 = x.right
x.right = y
y.left = T2
y.height = 1 + max(get_height(y.left), get_height(y.right))
x.height = 1 + max(get_height(x.left), get_height(x.right))
return x
def rotate_left(x):
y = x.right
T2 = y.left
y.left = x
x.right = T2
x.height = 1 + max(get_height(x.left), get_height(x.right))
y.height = 1 + max(get_height(y.left), get_height(y.right))
return y
def insert(node, val):
if not node:
return AVLTreeNode(val)
if val < node.val:
node.left = insert(node.left, val)
else:
node.right = insert(node.right, val)
node.height = 1 + max(get_height(node.left), get_height(node.right))
balance = get_balance(node)
if balance > 1 and val < node.left.val:
return rotate_right(node)
if balance < -1 and val > node.right.val:
return rotate_left(node)
if balance > 1 and val > node.left.val:
node.left = rotate_left(node.left)
return rotate_right(node)
if balance < -1 and val < node.right.val:
node.right = rotate_right(node.right)
return rotate_left(node)
return node
if not arr: return []
root = None
for val in arr:
root = insert(root, val)
return tree_to_list(root)Algorithm Pattern Checklist
When dealing with Trees data patterns.
- Are constraints clear?
- Is there a linear or logarithmic optimization possible?
Key Revision Notes
Standart Ağaçlar problem özellikleri geçerlidir.
İlgili Sorular
PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!
Önerilen Python Kaynakları
İlgili etkileşimli eğitimler, yardımcı sayfalar ve kod karşılaştırmalarıyla bilginizi genişletin.
Python Döngüleri
Veriler üzerinde yineleme yapmak için Python döngülerini nasıl kullanacağınızı öğrenin. Etkileşimli örneklerle for döngüleri, while döngüleri, kesme, devam etme ve döngü en iyi uygulamaları konusunda uzmanlaşın.
Python'da Liste Nasıl Sıralanır
Python'da sort() yöntemini ve sorted() işlevini kullanarak bir listeyi nasıl sıralayacağınızı öğrenin. Özel anahtar sıralama ve ters sıralama örneklerini keşfedin.
Python String Yöntemleri Hile Sayfası
Python dize manipülasyonu için eksiksiz bir başvuru kılavuzu. Dize özelliklerini biçimlendirme, arama, bölme, değiştirme ve denetleme konusunda uzmanlaşın.
Python vs JavaScript: Hangi Programlama Dili En İyisidir?
Python ve JavaScript arasında kapsamlı bir karşılaştırma. Sözdizimi farklılıklarını, performansı, kullanım örneklerini (arka uç ve ön uç) ve kodlama örneklerini keşfedin.