Wawancara 150 TeratasSedang

Pohon Biner Seimbang

Panduan terperinci dan implementasi Python untuk masalah 'Pohon Biner Seimbang'.

Pernyataan Masalah

Sedang

Diberikan pohon biner, tentukan apakah pohon tersebut seimbang tingginya.

Pohon biner dengan tinggi seimbang adalah pohon biner yang kedalaman dua subpohon dari setiap node tidak pernah berbeda lebih dari satu.

Pohon direpresentasikan sebagai daftar tingkat-urutan. Menerapkan fungsi isBalanced(root: list) -> bool.

Kendala
  • The number of nodes in the tree is in the range [0, 5000]
  • -10000 <= Node.val <= 10000

Contoh

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

The left subtree (rooted at 9) has depth 1, and the right subtree (rooted at 20) has depth 2. The difference is 1, so the tree is balanced.

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

The left subtree has depth 3 while the right subtree has depth 1. The difference is 2, so the tree is not balanced.

Example 3
Input
[]
Output
True
Explanation

An empty tree is considered balanced.

Need a Hint?
Pertimbangkan untuk menggunakan struktur data khusus Pohon seperti kumpulan atau tumpukan.
Edge Cases to Watch
  • Struktur masukan kosong
  • Masukan elemen tunggal
  • Batasan angka yang besar

Siap Memecahkannya?

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

Buka di Editor
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

Sumber Daya Python yang Direkomendasikan

Perluas pengetahuan Anda dengan tutorial interaktif terkait, lembar contekan, dan perbandingan kode.