def test_LCASame(self): root = Node(1) root.left = Node(2) root.right = Node(3) root.left.left = Node(4) root.left.right = Node(5) root.right.left = Node(6) root.right.right = Node(7) answer = LowestCommonAncestor.LCA(root, 7, 7) self.assertEqual(answer, 7) answer = LowestCommonAncestor.LCA(root, 4, 4) self.assertEqual(answer, 4)
def test_findRoute1(self): root = None route = None x = None answer = LowestCommonAncestor.findRoute(root, route, x) self.assertIs(answer, False)
def test_findRoute2(self): root = Node(1) route = [] x = 1 answer = LowestCommonAncestor.findRoute(root, route, x) self.assertIs(answer, True)
def test_LCARoot(self): root = Node(1) root.left = Node(2) root.right = Node(3) root.left.left = Node(4) root.left.right = Node(5) root.right.left = Node(6) root.right.right = Node(7) answer = LowestCommonAncestor.LCA(root, 1, 1) self.assertEqual(answer, 1)
def test_simple(self): root = Node(1) root.left = Node(2) root.right = Node(3) root.left.left = Node(4) root.left.right = Node(5) root.right.left = Node(6) root.right.right = Node(7) lca = LowestCommonAncestor.findLCA(root, 4, 5) assert lca.value is 2
def test_p_is_lowest(self): root = Node(3) root.left = Node(5) root.right = Node(1) root.left.left = Node(6) root.left.right = Node(2) root.right.left = Node(0) root.right.right = Node(8) root.left.right.left = Node(7) root.left.right.right = Node(4) lca = LowestCommonAncestor.findLCA(root, 5, 4) assert lca.value is 5
def test_LCA(self): root = Node(1) root.left = Node(2) root.right = Node(3) root.left.left = Node(4) root.left.right = Node(5) root.right.left = Node(6) root.right.right = Node(7) answer = LowestCommonAncestor.LCA(root, 2, 7) self.assertEqual(answer, 1) answer = LowestCommonAncestor.LCA(root, 2, 5) self.assertEqual(answer, 2) answer = LowestCommonAncestor.LCA(root, 4, 6) self.assertEqual(answer, 1) answer = LowestCommonAncestor.LCA(root, 6, 7) self.assertEqual(answer, 3) answer = LowestCommonAncestor.LCA(root, 4, 5) self.assertEqual(answer, 2) answer = LowestCommonAncestor.LCA(root, 5, 6) self.assertEqual(answer, 1)
def test_LCANone(self): root = None answer = LowestCommonAncestor.LCA(root, 7, 7) self.assertEqual(answer, -1)
def test_LCAOnlyRoot(self): root = Node(1) answer = LowestCommonAncestor.LCA(root, 1, 1) self.assertEqual(answer, 1)
def test_null(self): root = None lca = LowestCommonAncestor.findLCA(root, 4, 5) assert lca is None