def test_kth_smallest(self): self.assertEqual( 1, kthSmallest(createBinaryTreeFrom([3, 1, 4, None, 2]), 1)) self.assertEqual( 3, kthSmallest(createBinaryTreeFrom([5, 3, 6, 2, 4, None, None, 1]), 3))
def test_upside_down_binary_tree(self): self.assertEqual([4, 5, 2, None, None, 3, 1], toTreeArray( upsideDownBinaryTree( createBinaryTreeFrom([1, 2, 3, 4, 5])))) self.assertEqual([1], toTreeArray( upsideDownBinaryTree(createBinaryTreeFrom([1]))))
def test_serialize(self): codec = Codec() serialized = codec.serialize(createBinaryTreeFrom([2, 1, 3])) self.assertEqual('1 3 2', serialized) self.assertEqual([2, 1, 3], toTreeArray(codec.deserialize(serialized))) serialized = codec.serialize(createBinaryTreeFrom([])) self.assertEqual('', serialized) self.assertEqual([], toTreeArray(codec.deserialize(serialized)))
def test_is_valid_bst(self): self.assertEqual(True, isValidBSTPreorder(createBinaryTreeFrom([2, 1, 3]))) self.assertEqual( False, isValidBSTPreorder( createBinaryTreeFrom([5, 1, 4, None, None, 3, 6]))) self.assertEqual( False, isValidBSTPreorder( createBinaryTreeFrom([ 120, 70, 140, 50, 100, 130, 160, 20, 55, 75, 110, 119, 135, 150, 200 ]))) self.assertEqual( False, isValidBSTPreorder( createBinaryTreeFrom([5, 4, 6, None, None, 3, 7]))) self.assertEqual( False, isValidBSTPreorder(createBinaryTreeFrom([5, 14, None, 1]))) self.assertEqual(True, isValidBSTPreorder(createBinaryTreeFrom([0, -1]))) self.assertEqual(False, isValidBSTPreorder(createBinaryTreeFrom([2, 2, 2])))
def test_width_of_binary_tree(self): self.assertEqual( 4, widthOfBinaryTree(createBinaryTreeFrom([1, 3, 2, 5, 3, None, 9]))) self.assertEqual( 2, widthOfBinaryTree(createBinaryTreeFrom([1, 3, None, 5, 3])))
def test_max_depth(self): self.assertEqual(3, maxDepth(createBinaryTreeFrom([3, 9, 20, None, None, 15, 7]))) self.assertEqual(2, maxDepth(createBinaryTreeFrom([1, None, 2])))
def test_max_path_sum(self): self.assertEqual( 42, maxPathSum(createBinaryTreeFrom([-10, 9, 20, None, None, 15, 7]))) self.assertEqual(6, maxPathSum(createBinaryTreeFrom([1, 2, 3]))) self.assertEqual(2, maxPathSum(createBinaryTreeFrom([2, -1, -2])))
def test_InvertTree(self): # root = createBinaryTreeFrom([4, 2, 7, 1, 3, 6, 9]) # self.assertEquals([4,7,2,9,6,3,1], bfs(invertTree(root))) root = createBinaryTreeFrom([1, 2]) self.assertEquals([1, None, 2], invertTree(root))
def test_is_same_tree(self): self.assertEqual( False, isSameTree(createBinaryTreeFrom([0, -5]), createBinaryTreeFrom([0, -8])))
def test_largest_bstsubtree(self): self.assertEqual(3, largestBSTSubtree(createBinaryTreeFrom([10, 5, 15, 1, 8, None, 7]))) self.assertEqual(2, largestBSTSubtree( createBinaryTreeFrom([4, 2, 7, 2, 3, 5, None, 2, None, None, None, None, None, 1])))
def test_is_subtree(self): # self.assertEqual(True, isSubtree(createBinaryTreeFrom([3, 4, 5, 1, 2]), createBinaryTreeFrom([4, 1, 2]))) self.assertEqual( True, isSubtree(createBinaryTreeFrom([1, 1]), createBinaryTreeFrom([1])))