Example #1
0
    def test1(self):
        root = TreeNode.build_from_layerorder(
            [4, 1, 6, 0, 2, 5, 7, None, None, None, 3, None, None, None, 8])

        self.assertEqual([
            30, 36, 21, 36, 35, 26, 15, None, None, None, 33, None, None, None,
            8
        ], TreeNode.to_layerorder(self.bstToGst(root)))
 def test2(self):
     root = TreeNode(5)
     root.left = TreeNode(3)
     root.left.left = TreeNode(2)
     root.left.left.left = TreeNode(1)
     root.left.right = TreeNode(4)
     root.right = TreeNode(6)
     self.assertEqual(3, self.kthSmallest(root, 3))
 def test1(self):
     r = TreeNode(5)
     r.left = TreeNode(4)
     r.left.left = TreeNode(4)
     r.left.right = TreeNode(4)
     r.right = TreeNode(5)
     r.right.right = TreeNode(1)
     self.assertEqual(2, self.longestUnivaluePath(r))
Example #4
0
 def test2(self):
     root = TreeNode(5)
     root.left = TreeNode(1)
     root.right = TreeNode(4)
     root.right.left = TreeNode(3)
     root.right.right = TreeNode(6)
     self.assertEqual(False, self.isValidBST(root))
 def test1(self):
     root = TreeNode(1)
     root.left = TreeNode(2)
     root.right = TreeNode(3)
     root.right.left = TreeNode(4)
     root.right.left = TreeNode(5)
     self.assertEqual(preorder(root), preorder(self.deserialize(self.serialize(root))))
    def test1(self):
        root = TreeNode(7)
        root.left = TreeNode(3)
        root.right = TreeNode(15)
        root.right.left = TreeNode(9)
        root.right.right = TreeNode(20)

        iterator = BinarySearchTreeIterator(root)
        self.assertEqual(3, iterator.next())
        self.assertEqual(7, iterator.next())
        self.assertEqual(True, iterator.hasNext())
        self.assertEqual(9, iterator.next())
        self.assertEqual(15, iterator.next())
        self.assertEqual(20, iterator.next())
        self.assertEqual(False, iterator.hasNext())
 def test1(self):
     root = TreeNode(3)
     root.left = TreeNode(1)
     root.left.right = TreeNode(2)
     root.right = TreeNode(4)
     self.assertEqual(1, self.kthSmallest(root, 1))
 def test3(self):
     s = TreeNode.build_from_layerorder([2])
     t = TreeNode.build_from_layerorder([12])
     self.assertEqual(False, self.isSubtree(s, t))
 def test2(self):
     s = TreeNode.build_from_layerorder(
         [3, 4, 5, 1, 2, None, None, None, 0])
     t = TreeNode.build_from_layerorder([4, 1, 2])
     self.assertEqual(False, self.isSubtree(s, t))
 def test1(self):
     s = TreeNode.build_from_layerorder([3, 4, 5, 1, 2])
     t = TreeNode.build_from_layerorder([4, 1, 2])
     self.assertEqual(True, self.isSubtree(s, t))
Example #11
0
 def test2(self):
     root = TreeNode.build_from_layerorder([-10,9,20,None,None,15,7])
     self.assertEqual(42, self.maxPathSum(root))
Example #12
0
 def test1(self):
     root = TreeNode.build_from_preorder([1,2,3])
     self.assertEqual(6, self.maxPathSum(root))
Example #13
0
 def test1(self):
     root = TreeNode(2)
     root.left = TreeNode(1)
     root.right = TreeNode(3)
     self.assertEqual(True, self.isValidBST(root))
 def test1(self):
     root = TreeNode(1)
     root.left = TreeNode(2)
     root.left.right = TreeNode(5)
     root.right = TreeNode(3)
     self.assertEqual(["1->2->5", "1->3"], self.binaryTreePaths(root))
 def dec(s):
     return None if s == 'n' else TreeNode(int(s))