Tree - Same Tree
All diagrams presented herein are original creations, meticulously designed to enhance comprehension and recall. Crafting these aids required considerable effort, and I kindly request attribution if this content is reused elsewhere.
Difficulty : Easy
Recursion
Problem
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.
Example 1:
1
2
Input: p = [1,2,3], q = [1,2,3]
Output: true
Example 2:
1
2
Input: p = [1,2], q = [1,null,2]
Output: false
Example 3:
1
2
Input: p = [1,2,1], q = [1,1,2]
Output: false
Final Code
Here is the full code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
def same_tree(p:TreeNode, q:TreeNode):
# Return None if both the nodes
# are None.
if not p and not q:
return True
# If previous if condition is not
# True then its possible for one
# of the node to be None while
# other one is not None.
if not p or not q:
return False
# Return False if the values
# do not match
if p.val != q.val:
return False
#Traverse left and right node and return
return same_tree(p.left, q.left) and same_tree(p.right, q.right)
This post is licensed under CC BY 4.0 by the author.