# Andela Qualified.io assessment: Question 2

Given the following class:

``````class TreeNode:
def __init__(self, val, left=None, right=None):
self.val = val
self.left = left
self.right = right
``````

complete the function

which compares the two trees defined by `TreeNode``a` and `b` and returns `true` if they are equal in structure and in value and `false` otherwise.

#### My solution

``````# return True if the two binary trees rooted and a and b are equal in value and structure
# return False otherwise
def compare(a, b):
#Where both trees are empty
if a is None and b is None:
return True
elif a is None and b != None:
return False
elif a != None and b is None:
return False
#if the root values are equal, recurse for left and right nodes
else:
if a.val == b.val and compare(a.left, b.left) and compare(a.right, b.right):
return True
else:
return False``````

#### My tests

``````import unittest

class Test(unittest.TestCase):
def test_should_return_true_for_equal_nodes(self):
self.assertTrue(compare(a_node, b_node))
def test_should_return_false_for_non_equal_nodes(self):
self.assertFalse(compare(a_node, c_node))

class Node:
def __init__(self, val, left, right):
self.val = val
self.left = left
self.right = right

a_node = Node(1, None, None)
b_node = Node(1, None, None)
c_node = Node(2, None, None)``````