def test_max_path_sum__example1(self): """ Example 1: Input: root = [1,2,3] Output: 6 """ root = TreeNode.of([1, 2, 3]) res = BinaryTreeMaxPathSum().maxPathSum(root) self.assertEqual(6, res)
def test_max_path_sum__example2(self): """ Example 2: Input: root = [-10,9,20,null,null,15,7] Output: 42 """ root = TreeNode.of([-10, 9, 20, None, None, 15, 7]) res = BinaryTreeMaxPathSum().maxPathSum(root) self.assertEqual(42, res)
def test_lowest_common_ancestor_example3(self): """ Example 3: Input: root = [1,2], p = 1, q = 2 Output: 1 """ res = LowestCommonAncestor().lowestCommonAncestor(root=TreeNode.of( [1, 2]), p=TreeNode(1), q=TreeNode(2)) self.assertEqual(1, res.val)
def test_lowest_common_ancestor_example1(self): """ Example 1: Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1 Output: 3 Explanation: The LCA of nodes 5 and 1 is 3. """ res = LowestCommonAncestor().lowestCommonAncestor(root=TreeNode.of( [3, 5, 1, 6, 2, 0, 8, None, None, 7, 4]), p=TreeNode(5), q=TreeNode(1)) self.assertEqual(3, res.val)
def test_lowest_common_ancestor_example2(self): """ Example 2: Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4 Output: 5 Explanation: The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition. """ res = LowestCommonAncestor().lowestCommonAncestor(root=TreeNode.of( [3, 5, 1, 6, 2, 0, 8, None, None, 7, 4]), p=TreeNode(5), q=TreeNode(4)) self.assertEqual(5, res.val)
def test_sample3(self): res = BinaryTreeMaxPathSum().maxPathSum( TreeNode.of( [9, 6, -3, None, None, -6, 2, None, None, 2, None, -6, -6, -6])) self.assertEqual(16, res)
def test_max_1_neg2_neg3(self): res = BinaryTreeMaxPathSum().maxPathSum(TreeNode.of([1, -2, -3])) self.assertEqual(1, res)
def test_max_1_3_neg10_neg2_none(self): res = BinaryTreeMaxPathSum().maxPathSum( TreeNode.of([1, 3, -10, -2, None])) self.assertEqual(4, res)