Entrevista a los 150 mejoresMedio

Mismo árbol binario

Guía detallada e implementación Python para el problema 'Mismo árbol binario'.

Declaración del problema

Medio

Dadas las raíces de dos árboles binarios p y q, escribe una función para comprobar si son iguales o no.

Dos árboles binarios se consideran iguales si son estructuralmente idénticos y los nodos tienen el mismo valor.

Los árboles se representan como listas de orden de niveles. Implementar una función isSameTree(p: list, q: list) -> bool.

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

Ejemplos

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 la posibilidad de utilizar estructuras de datos específicas de Trees, como conjuntos o montones.
Edge Cases to Watch
  • Estructuras de entrada vacías
  • Entradas de un solo elemento
  • Grandes límites numéricos

¿Listo para resolver?

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

Abrir en el 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 recomendados de Python

Amplíe sus conocimientos con tutoriales interactivos relacionados, hojas de trucos y comparaciones de códigos.