/100. Same Tree

100. Same Tree

Easy
Trees66.8% acceptance

Given the roots of two binary trees root_a and root_b, determine if the two trees are structurally identical and all corresponding nodes have equal values. Return True if they are identical, otherwise return False.

Example 1

Input: root_a = TreeNode(4, TreeNode(2), TreeNode(6)) root_b = TreeNode(4, TreeNode(2), TreeNode(6))

Output: True

Explanation: Both trees have the same structure and node values.

Example 2

Input: root_a = TreeNode(5, TreeNode(3), None) root_b = TreeNode(5, None, TreeNode(3))

Output: False

Explanation: The structure of the trees is different.

Example 3

Input: root_a = None root_b = None

Output: True

Explanation: Both trees are empty.

Constraints

  • 0 <= number of nodes in each tree <= 100
  • -10_000 <= TreeNode.val <= 10_000
Python (current runtime)

Case 1

Input: root_a = TreeNode(7, TreeNode(1), TreeNode(9)) root_b = TreeNode(7, TreeNode(1), TreeNode(9) )

Expected: True

Case 2

Input: root_a = TreeNode(8, TreeNode(3, TreeNode(1)), TreeNode(10)) root_b = TreeNode(8, TreeNode(3, None), TreeNode(10))

Expected: False

Case 3

Input: root_a = TreeNode(2) root_b = TreeNode(2)

Expected: True

Case 4

Input: root_a = None root_b = TreeNode(0)

Expected: False