Wawancara 150 TeratasSedang

Pohon Biner yang Sama

Panduan terperinci dan implementasi Python untuk masalah 'Pohon Biner yang Sama'.

Pernyataan Masalah

Sedang

Diketahui akar dari dua pohon biner p dan q, tulislah sebuah fungsi untuk memeriksa apakah keduanya sama atau tidak.

Dua pohon biner dianggap sama jika keduanya identik secara struktural, dan node-nodenya memiliki nilai yang sama.

Pohon-pohon direpresentasikan sebagai daftar tingkat-urutan. Menerapkan fungsi isSameTree(p: list, q: list) -> bool.

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

Contoh

Example 1
Input
[1,2,3], [1,2,3]
Output
True
Explanation

Both trees have the same structure and values.

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

In the first tree, 2 is the left child. In the second tree, 2 is the right child. Different structures.

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

Same structure but different values at the children.

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.