if not first_tree:
        return False

    if not second_tree:
        return False

    if first_tree.value == second_tree.value:
        left_is_equal = is_same_tree(first_tree.left, second_tree.left)
        right_is_equal = is_same_tree(first_tree.right, second_tree.right)

        if left_is_equal and right_is_equal:
            return True

    return False


if __name__ == '__main__':
    root1 = TreeNode(1)
    root1.left = TreeNode(2)
    root1.right = TreeNode(3)

    root2 = TreeNode(1)
    root2.left = TreeNode(2)
    root2.right = TreeNode(3)
    assert is_same_tree(root1, root2) == True

    root2.right.right = TreeNode(4)
    assert is_same_tree(root1, root2) == False

    print("All test cases passed.")
Exemplo n.º 2
0
"""
Given a binary tree, find its maximum depth.

The maximum depth is the number of nodes along the longest path from the root 
node down to the farthest leaf node.
"""

from trees import TreeNode
from trees import BinaryTree


def maximum_depth(node):
    if not node:
        return 0

    return 1 + max(maximum_depth(node.left), maximum_depth(node.right))


if __name__ == '__main__':
    root_node = TreeNode(10)
    root_node.left = TreeNode(12)
    root_node.right = TreeNode(24)
    root_node.left.left = TreeNode(1)
    root_node.left.left.left = TreeNode(1)
    binary_tree = BinaryTree(root_node)

    assert maximum_depth(binary_tree.root) == 4
    print("All test cases passed.")