Tap here to switch tabs
Problem
Ask
Submissions

Problem: Same Tree

easy
15 min
Understand how to compare two binary trees to determine if they are structurally identical with matching node values. Learn to implement an efficient O(n) solution and apply coding patterns to this common interview question.

Statement

Given the roots of two binary trees, p and q, write a function to check whether or not they are the same. Two binary trees are considered the same if they’re structurally identical and the nodes have the same value.

Constraints:

  • The number of nodes in the tree is in the range [0,100][0, 100]

  • 104-10^4 \leq node.data 104\leq 10^4

Tap here to switch tabs
Problem
Ask
Submissions

Problem: Same Tree

easy
15 min
Understand how to compare two binary trees to determine if they are structurally identical with matching node values. Learn to implement an efficient O(n) solution and apply coding patterns to this common interview question.

Statement

Given the roots of two binary trees, p and q, write a function to check whether or not they are the same. Two binary trees are considered the same if they’re structurally identical and the nodes have the same value.

Constraints:

  • The number of nodes in the tree is in the range [0,100][0, 100]

  • 104-10^4 \leq node.data 104\leq 10^4