Same Tree
Tree
easy
Score: 20
Given the roots of two binary trees p
and q
, write a function to check if they are the same or not. Return 1 if they are same else 0.
Two binary trees are considered the same if they are structurally identical, and the nodes have the same value.
Class TreeNode:
val (int)
left (TreeNode)
right (TreeNode)
Input Format
First Parameter - TreeNode p
Second Parameter - TreeNode q
Output Format:
Return the number.
Example 1:
Input:
1 2 3
1 2 3
Output: 1
Example 2
Input:
1 2
1 null 2
Output: 0
Constraints
- The number of nodes in both trees is in the range
[0, 100]
. - -104 <=
Node.val
<= 104 - Expected Time Complexity: O(n)
- Expected Space Complexity: O(n)