Question

Given the roots of two binary trees p and q, write a function to check if they are the same or not.

Two binary trees are considered the same if they are structurally identical, and the nodes have the same value.

This is a tree question.

Idea(s)

  • DFS
    • Keep a list per tree that will hold the values
    • We need to check node values, and orientations
    • Iterate through both trees using DFS appending “None” to a tree list if there is no child in one direction
    • This will yield two lists with values of nodes and “None”s in order form left to right child
  • Return the equality of both lists

Solution