Exemplo n.º 1
0
# -*- coding:utf-8 -*-
""" 输入一棵二叉树和一个整数,打印出二叉树中结点值的和为该整数的所有路径。
    路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。"""

from BinaryTree.base_tree_node import gen_a_tree


def path(a, num):

    if not a:
        return []
    if not a.left and not a.right and a.value == num:
        return [[a.value]]

    res = []
    left = path(a.left, num - a.value)
    right = path(a.right, num - a.value)

    for i in left + right:
        print([a.value] + i)
        res.append([a.value] + i)
    return res


if __name__ == '__main__':

    root = gen_a_tree()
    print(path(root, 10))
Exemplo n.º 2
0
    if not node and not mirror_node:
        return True
    elif not all((node, mirror_node)):
        return False
    else:
        if node.value == mirror_node.value:
            return symmetrical(node.left, node.left) and symmetrical(
                node.left, node.right)
        else:
            return False


if __name__ == '__main__':

    root = gen_a_tree()  # root为非对称的测试用例
    root2 = gen_a_tree()
    mir = mirror(root2)

    print(symmetrical(root, root2))

    root3 = TreeNode(1)  # root3为对称的测试用例
    node1 = TreeNode(2)
    node2 = TreeNode(2)
    root3.left = node1
    root3.right = node2

    root4 = root3
    mirror(root4)
    print(symmetrical(root3, root4))
Exemplo n.º 3
0
    left = dfs(root.left)
    right = dfs(root.right)

    return max(left, right) + 1


def bfs(root):

    # bfs遍历 利用队列将每层的跟节点逐一打印
    if not root:
        return 0
    my_queue = []
    node = root
    my_queue.append(node)

    while my_queue:
        node = my_queue.pop(0)
        print(node.value)
        if node.left is not None:
            my_queue.append(node.left)
        if node.right is not None:
            my_queue.append(node.right)


if __name__ == '__main__':

    a = gen_a_tree()
    print(dfs(a))
    # bfs(a)
Exemplo n.º 4
0
def depth_of_tree(root):
    """ 获取树的深度 """

    if not root:
        return -1
    left = depth_of_tree(root.left)
    right = depth_of_tree(root.right)

    return max(left, right) + 1


def is_balance(root):

    if not root:
        return False
    else:
        left_depth = depth_of_tree(root.left)
        right_depth = depth_of_tree(root.right)

    if abs(left_depth - right_depth) > 1:
        return False
    return True


if __name__ == '__main__':

    node = gen_a_tree()

    print(depth_of_tree(node))
    print(is_balance(node))
Exemplo n.º 5
0
from BinaryTree.base_tree_node import gen_a_tree
""" 从上往下打印出二叉树的每个节点,同层节点从左至右打印。 """
""" 思路 遍历时 使用一个队列来存储将要打印的结点 利用先进先出的特点 依次打印队列中的元素"""


def top2bottom_print(root):

    if not root:
        return []
    task = Queue()
    task.put(root)
    result = []

    while not task.empty():
        node = task.get()
        root = node
        result.append(node.value)

        if root.left:
            task.put(root.left)
        if root.right:
            task.put(root.right)
    return result


if __name__ == '__main__':

    root_node = gen_a_tree()
    res = top2bottom_print(root_node)
    print(res)