150 principais entrevistasMédio

Mesma árvore binária

Guia detalhado e implementação de Python para o problema da 'Mesma Árvore Binária'.

Declaração do problema

Médio

Dadas as raízes de duas árvores binárias p e q, escreva uma função para verificar se elas são iguais ou não.

Duas árvores binárias são consideradas iguais se forem estruturalmente idênticas e os nós tiverem o mesmo valor.

As árvores são representadas como listas de ordem de nível. Implemente uma função isSameTree(p: list, q: list) -> bool.

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

Exemplos

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?
Considere usar estruturas de dados específicas de árvores, como conjuntos ou heaps.
Edge Cases to Watch
  • Estruturas de entrada vazias
  • Entradas de elemento único
  • Grandes limites numéricos

Pronto para resolver?

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

Abrir no 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

Recursos Python recomendados

Expanda seu conhecimento com tutoriais interativos relacionados, folhas de dicas e comparações de código.