Ejemplo n.º 1
0
 def test_findDuplicateSubtrees(self):
     testcases = [[1, 2, 3, 4, null, 2, 4, null, null, 4], [2, 1, 1]]
     for testcase in testcases:
         print(testcase)
         tree = createTree(testcase)
         nodes = self.findDuplicateSubtrees(tree)
         print([node.val for node in nodes])
Ejemplo n.º 2
0
 def test_postorderTraversal(self):
     testCases = [[1,null,2,3], [], [1]]
     for testCase in testCases:
         print(testCase)
         tree = createTree(testCase)
         print(self.preorderTraversal(tree))
         print("\n\n")
Ejemplo n.º 3
0
 def test_maxDepth(self):
     testcases = [
         [3, 9, 20, null, null, 15, 7],
     ]
     for testcase in testcases:
         print(testcase)
         root = createTree(testcase)
         print(self.maxDepth(root))
Ejemplo n.º 4
0
 def test_isBalanced(self):
     testcases = [
         [1, 2, 3, 4, 5, 6, null, 8],
     ]
     for testcase in testcases:
         print(testcase)
         root = createTree(testcase)
         print(self.isBalanced(root))
Ejemplo n.º 5
0
 def test_invertTree(self):
     testcases = [
         [4, 2, 7, 1, 3, 6, 9],
     ]
     for testcase in testcases:
         print(testcase)
         root = createTree(testcase)
         self.invertTree(root).printTree()
Ejemplo n.º 6
0
 def test_levelOrderBottom(self):
     testcases = [
         [3, 9, 20, 1, null, 15, 7],
     ]
     for testcase in testcases:
         print(testcase)
         root = createTree(testcase)
         print(self.levelOrderBottom(root))
Ejemplo n.º 7
0
 def test_binaryTreePaths(self):
     testcases = [
         [1, 2, 3, null, 5],
     ]
     for test in testcases:
         t = createTree(test)
         t.printTree()
         print(self.binaryTreePaths(t))
Ejemplo n.º 8
0
 def test_hasPathSum(self):
     testcases = [([5,4,8,11,null,13,4,7,2,null,null,null,1], 22), ([1,2], 1), ([], 0)]
     for testcase in testcases:
         print(testcase)
         treeValue, sum = testCase
         tree = createTree(treeValue)
         result = self.hasPathSum(tree, sum)
         tree.printTree() if tree else print("[]")
         print(sum)
         print("Result : %s\n\n" %result)
Ejemplo n.º 9
0
 def test_lowestCommonAncestor(self):
     tests = [([6, 2, 8, 0, 4, 7, 9, null, null, 3, 5], 2, 8),
              ([6, 2, 8, 0, 4, 7, 9, null, null, 3, 5], 2, 4), ([2,
                                                                 1], 2, 1)]
     for test in tests:
         tree, p, q = test
         tree = createTree(tree)
         tree.printTree()
         p = TreeNode(p)
         q = TreeNode(q)
         lca = self.lowestCommonAncestor(tree, p, q)
         if lca:
             print(lca.val)
         else:
             print("no lca")