def test_issymmetric4(self): a1 = source.TreeNode(1) a2 = source.TreeNode(2) a3 = source.TreeNode(2) a1.left = a2 a1.right =a3 self.assertEqual(source.is_symmetric_iterative(a1),True)
def test_issubtree2(self): a1 = source.TreeNode(4) a2 = source.TreeNode(11) a3 = source.TreeNode(1) a1.left = a2 a1.right =a3 b1 = createNode() self.assertEqual(source.is_subtree(b1,a1),False)
def test_array2bst(self): a1 = source.TreeNode(5) a2 = source.TreeNode(2) a3 = source.TreeNode(7) a1.left = a2 a1.right = a3 nums = [2,5,7] x = source.array2bst(nums) self.assertEqual(x,a1)
def test_deepestleft2(self): root = source.TreeNode(1) root.right = source.TreeNode(3) root.right.left = source.TreeNode(4) root.right.right = source.TreeNode(6) root.right.right.right = source.TreeNode(7) d=source.DeepestLeft() source.find_deepest_left(root,True,1,d) self.assertEqual(d.Node,root.right.left) self.assertEqual(d.depth,3)
def createNode(): a1 = source.TreeNode(5) a2 = source.TreeNode(4) a3 = source.TreeNode(8) a4 = source.TreeNode(11) a5 = source.TreeNode(13) a6 = source.TreeNode(4) a7 = source.TreeNode(7) a8 = source.TreeNode(2) a9 = source.TreeNode(5) a10 = source.TreeNode(1) a1.left = a2 a1.right =a3 a2.left = a4 a3.left = a5 a3.right = a6 a4.left = a7 a4.right = a8 a6.left = a9 a6.right = a10 return a1
def test_reverse(self): a1 = source.TreeNode(1) a2 = source.TreeNode(4) a3 = source.TreeNode(5) a4 = source.TreeNode(7) a5 = source.TreeNode(9) a1.left = a2 a1.right = a3 a1.left.left = a4 a3.right = a5 b1 = source.TreeNode(1) b2 = source.TreeNode(5) b3 = source.TreeNode(4) b4 = source.TreeNode(7) b5 = source.TreeNode(9) b1.left = b2 b1.right = b3 b1.right.right = b4 b2.left = b5 source.reverse(a1) self.assertEqual(a1,b1)
def test_LCA(self): a1 = source.TreeNode(5) a2 = source.TreeNode(4) a3 = source.TreeNode(8) a4 = source.TreeNode(11) a5 = source.TreeNode(13) a6 = source.TreeNode(4) a7 = source.TreeNode(7) a8 = source.TreeNode(2) a9 = source.TreeNode(5) a10 = source.TreeNode(1) a1.left = a2 a1.right =a3 a2.left = a4 a3.left = a5 a3.right = a6 a4.left = a7 a4.right = a8 a6.left = a9 a6.right = a10 self.assertEqual(source.LCA(a1,a5,a10),a3)
def test_isbalanced4(self): a1 = source.TreeNode(1) a2 = source.TreeNode(2) a3 = source.TreeNode(2) a4 = source.TreeNode(3) a5 = source.TreeNode(4) a6 = source.TreeNode(4) a7 = source.TreeNode(3) a1.left = a2 a1.right =a3 a2.left = a4 a2.right = a5 a3.left = a6 a3.right = a7 self.assertEqual(source.is_balanced_2(a1),True)
def test_issymmetric2(self): a1 = source.TreeNode(1) a2 = source.TreeNode(2) a3 = source.TreeNode(2) a4 = source.TreeNode(3) a5 = source.TreeNode(4) a6 = source.TreeNode(4) a7 = source.TreeNode(3) a1.left = a2 a1.right =a3 a2.left = a4 a2.right = a5 a3.left = a6 a3.right = a7 self.assertEqual(source.is_symmetric(a1),True)
def test_isSameTree_False(self): a1 = source.TreeNode(30) a2 = source.TreeNode(40) a3 = source.TreeNode(50) a1.left = a2 a1.right = a3 b1 = source.TreeNode(30) b2 = source.TreeNode(90) b3 = source.TreeNode(50) b1.left = b2 b1.right = b3 self.assertEqual(source.isSameTree(a1,b1),False)