def find_second_largest(root):
    arr = [TreeNode(-math.inf), TreeNode(-math.inf)]
    traverse_tree(root, arr)
    if arr[1] == -math.inf:
        # the tree has 0 or 1 elements
        return None
    return arr[1]
示例#2
0
    current_path = left_result[1] + node.val + right_result[1]
    # find the max path till now, comparing the new path, max path from the left and right subtree
    max_path = max(left_result[0], current_path, right_result[0])
    # find the max subpath, min value for a subpath sum is 0
    max_subpath = max(left_result[1] + node.val, right_result[1] + node.val, node.val, 0)

    return (max_path, max_subpath)


###########
# Testing #
###########

# Test 1
# Correct result => 18
tree = TreeNode(1, TreeNode(2, TreeNode(4), TreeNode(5)), TreeNode(3, TreeNode(6), TreeNode(7)))
print(max_path_sum(tree))

# Test 2
# Correct result => 10
tree = TreeNode(-1, TreeNode(-2, TreeNode(-4), TreeNode(-5)), TreeNode(3, TreeNode(2), TreeNode(5)))
print(max_path_sum(tree))

# Test 3
'''
        1
       / \
      7   3
     / \ / \
   -4 -5 6  2
'''
示例#3
0
        if not root: return 0
        l = self.maxHeight(root.left)
        r = self.maxHeight(root.right)
        return max(l, r) + 1


if __name__ == "__main__":

    '''
        3            # height 1
       / \
      9  20          # height 2
        /  \
       15   7        # height 3
    '''
    root1 = TreeNode(3)
    root1.left = TreeNode(9)
    root1.right = TreeNode(20)
    root1.right.left = TreeNode(15)
    root1.right.right = TreeNode(7)

    '''
            3            # height 1
           / \
          9  20          # height 2
            /  \
           15   7        # height 3
          / \
         13  19          # height 4
        /
       10                # height 5
    if arr[1] == -math.inf:
        # the tree has 0 or 1 elements
        return None
    return arr[1]


def traverse_tree(node, arr):
    if node == None:
        return

    if arr[0].val < node.val:
        arr[1] = arr[0]
        arr[0] = node
    elif arr[1].val < node.val:
        arr[1] = node

    # search left
    traverse_tree(node.left, arr)
    # search right
    traverse_tree(node.right, arr)


###########
# Testing #
###########

# Test 1
# Correct result => 8
tree = TreeNode(1, TreeNode(5, TreeNode(2), TreeNode(8)),
                TreeNode(4, TreeNode(12), TreeNode(7)))
print(find_second_largest(tree).val)

###########
# Testing #
###########

# Test 1
'''
    5
   / \
  1   4
     / \
    3   6
'''
# Correct result => False
root = TreeNode(5, TreeNode(1), TreeNode(4, TreeNode(3), TreeNode(6)))
print(is_valid_bst(root))

# Test 2
'''
    5
   / \
  1   6
     / \
    4   7
'''
# Correct result => False
root = TreeNode(5, TreeNode(1), TreeNode(6, TreeNode(4), TreeNode(7)))
print(is_valid_bst(root))

# Test 3
    k = right[0] - 1
    if k == 0:
        return (0, node)

    # check left
    return search_2(node.left, k)


###########
# Testing #
###########

# Test 1
# Correct result => 10
tree = TreeNode(
    5, TreeNode(3, TreeNode(1), TreeNode(4)),
    TreeNode(8, TreeNode(7), TreeNode(12, TreeNode(10, TreeNode(13)))))
print(find_second_largest_bst_1(tree).val)
print(find_second_largest_bst_2(tree).val)

# Test 2
# Correct result => 8
tree = TreeNode(5, TreeNode(3, TreeNode(1), TreeNode(4)),
                TreeNode(8, TreeNode(7), TreeNode(12)))
print(find_second_largest_bst_1(tree).val)
print(find_second_largest_bst_2(tree).val)

# Test 3
# Correct result => 4
tree = TreeNode(5, TreeNode(3, TreeNode(1), TreeNode(4)))
print(find_second_largest_bst_1(tree).val)
示例#7
0
        left_result = total_unival_trees(node.left)
        unival_trees += left_result[0]
        is_left_unival_tree = left_result[1]
        left_value = node.left.val

    # count right unival trees and save the value
    if node.right is not None:
        right_result = total_unival_trees(node.right)
        unival_trees += right_result[0]
        is_right_unival_tree = right_result[1]
        right_value = node.right.val

    # check if this root is an unival tree
    is_this_unival_tree = is_left_unival_tree and is_right_unival_tree and (
        left_value == right_value)
    unival_trees += is_this_unival_tree

    return (unival_trees, is_this_unival_tree)


###########
# Testing #
###########

# Test 1
# Correct result => 5
print(
    count_unival_trees(
        TreeNode(
            0, TreeNode(1),
            TreeNode(0, TreeNode(1, TreeNode(1), TreeNode(1)), TreeNode(0)))))
示例#8
0
    queue.append((root, 0))

    while queue:
        node, lvl = queue.popleft()

        if node is None:
            continue

        if len(results) < lvl + 1:
            results.append([])
        results[lvl].append(node.val)

        lvl += 1
        queue.append((node.left, lvl))
        queue.append((node.right, lvl))

    # reverse odd level
    for i in range(1, len(results), 2):
        results[i] = results[i][::-1]

    return results


###########
# Testing #
###########

# Test 1
# Correct result => [[3], [20, 9], [15, 7]]
tree = TreeNode(3, TreeNode(9), TreeNode(20, TreeNode(15), TreeNode(7)))
print(zigzag_level_order_traversal(tree))
def find_diameter(root):
    ''' returns (max branch length, max diameter) '''
    if not root:
        return 0, 0

    # traverse left and right subtrees
    left, right = find_diameter(root.left), find_diameter(root.right)

    # return the max branch from the left and right subtrees plus the current node
    # and find the max diameter till now (using the current node and the max left and right subtree branches)
    return max(left[0], right[0]) + 1, max(left[1], right[1],
                                           left[0] + right[0] + 1)


###########
# Testing #
###########

# Test 1
# Correct result => 6
tree = TreeNode(3, TreeNode(1, None, TreeNode(2, TreeNode(7))),
                TreeNode(4, None, TreeNode(5)))
print(diameter(tree))

# Test 2
# Correct result => 5
tree = TreeNode(
    5, TreeNode(3, TreeNode(2, TreeNode(1)), TreeNode(4, None, TreeNode(8))),
    TreeNode(6))
print(diameter(tree))
示例#10
0
        return False

    if p.val != q.val:
        return False

    # check left
    if not is_same_tree(p.left, q.left):
        return False

    # check right
    if not is_same_tree(p.right, q.right):
        return False

    return True


###########
# Testing #
###########

# Test 1
# Correct result => True
p = TreeNode(1, TreeNode(2), TreeNode(3))
q = TreeNode(1, TreeNode(2), TreeNode(3))
print(is_same_tree(p, q))

# Test 2
# Correct result => False
p = TreeNode(1, TreeNode(2))
q = TreeNode(1, None, TreeNode(2))
print(is_same_tree(p, q))
'''


############
# Solution #
############

# import TreeNode class from tree_helpers.py
from tree_helpers import TreeNode

def max_branch_sum(node):
    if node is None:
        return 0

    # take the max left subbranch sum and add the current node value
    left_max_sum = max_branch_sum(node.left) + node.val
    # take the max right subbranch sum and add the current node value
    right_max_sum = max_branch_sum(node.right) + node.val

    # return the bigger sum
    return max(left_max_sum, right_max_sum)


###########
# Testing #
###########

# Test 1
# Correct result => 11
tree = TreeNode(1, TreeNode(2, TreeNode(4), TreeNode(5)), TreeNode(3, TreeNode(6), TreeNode(7)))
print(max_branch_sum(tree))