Created
April 13, 2019 18:50
-
-
Save phalbert/2ac200625ca8cc5276e5d2c1c70d0864 to your computer and use it in GitHub Desktop.
Compares the two trees defined by Nodes a and b and returns true if they are equal in structure and in value and false otherwise.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if a == b: | |
return True | |
if a is None or b is None: | |
return False | |
if a.val != b.val: | |
return False | |
return compare(a.left, b.left) and compare(a.right, b.right) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
from node_compare import compare, Node | |
a_node = Node(1, None, None) | |
b_node = Node(1, None, None) | |
c_node = Node(2, None, None) | |
d_node = Node(1, Node(1, None, None), None) | |
e_node = Node(1, Node(1, None, None), None) | |
f_node = Node(1, Node(1, None, Node(1, None, None)), None) | |
g_node = Node(1, Node(1, None, Node(1, None, None)), None) | |
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)) | |
def test_should_handle_depth_of_one(self): | |
self.assertFalse(compare(d_node, None)) | |
def test_should_handle_depth_of_two(self): | |
self.assertTrue(compare(f_node, g_node)) | |
if __name__ == '__main__': | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment