Same Tree

Write the code, see it run, pass the checks.

The problem

  • easy
  • Code

Write isSameTree(a, b). Each is the root of a binary tree made of nodes { value, left, right }, where left and right are child nodes or null; an empty tree is null. Return true if the two trees have exactly the same shape and the same value at every position, comparing values with ===, and false otherwise. Don't change either tree. For example, two trees written in level order as [1, 2, 3] are the same, but [1, 2] and [1, null, 2] are not, even with the same values: the 2 is a left child in one and a right child in the other.

Your code must pass

  • Returns true for matching trees
  • Returns false when a value differs
  • Returns false when the shape differs
  • Handles empty trees