Junior — Senior
Adjustment of the binary trees comparison function
livecode
Task condition
Analyze the provided code of the is_equal function. Identify and correct the logical flaws that lead to incorrect determination of the identity of two binary trees TreeNode. After correction, the function should return True only if both structures completely match in node placement and their values; otherwise, it should return False.
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
def is_equal(obj_1: TreeNode, obj_2: TreeNode) -> bool:
if obj_1 is None and obj_2 is None:
return True
if obj_1 is None or obj_2 is None:
return False
if obj_1.val != obj_2.val:
return False
left = is_equal(obj_1.left, obj_2.left)
right = is_equal(obj_1.right, obj_2.right)
return left and right