root: 根节点
        '''
        if root.left:
            self.helper(result, root.left)
        if root.right:
            self.helper(result, root.right)
        result.append(root.val)

    def post_order_traversal(self, root: TreeNode) -> List[int]:
        '''
            后序遍历
        Args:
            res: 链表
            root: 根节点
        '''
        result = []
        if not root:
            return result
        self.helper(result, root)
        return result


if __name__ == '__main__':
    arr = [1, 2, 3, 4, 5]
    tree_node = TreeNode()
    tree = tree_node.create_binary_tree_array(arr)
    solution = Solution()
    result = solution.post_order_traversal(tree)
    print(result)
    assert result[len(result) - 1] == 1
Пример #2
0
        Returns:
            布尔值
        '''
        queue = []
        queue.append(root)
        queue.append(root)
        while queue:
            r1 = queue.pop()
            r2 = queue.pop()
            if not r1 and not r2:
                continue
            if not r1 or not r2:
                return False
            if r1.val != r2.val:
                return False
            queue.append(r1.left)
            queue.append(r2.right)
            queue.append(r1.right)
            queue.append(r2.left)
        return True


if __name__ == '__main__':
    arr1 = [1, 2, 2]
    tree_node = TreeNode()
    root = tree_node.create_binary_tree_array(arr1)
    solution = Solution()
    result = solution.is_symmetric2(root)
    print(result)
    assert result == True
        result = 0
        queue = []
        queue_index = []
        queue.append(root)
        queue_index.append(1)
        while len(queue) > 0:
            level_size = len(queue)
            init_index = queue_index[0]
            i = init_index
            while level_size > 0:
                r = queue.pop(0)
                i = queue_index.pop(0)
                if r.left:
                    queue.append(r.left)
                    queue_index.append(i * 2)
                if r.right:
                    queue.append(r.right)
                    queue_index.append(i * 2 + 1)
                level_size -= 1
            result = max(result, i - init_index + 1)
        return result


if __name__ == '__main__':
    arr = [3, 2, 3, None, 3, None, 1]
    node = TreeNode.create_binary_tree_array(arr)
    solution = Solution()
    result = solution.width_of_binary_tree(node)
    print(result)
    assert result == 3
Пример #4
0
    '''
    if t1 == None:
        return t2
    if t2 == None:
        return t1
    stack = []
    stack.append((t1, t2))
    while stack:
        t = stack.pop()
        if t[0] == None or t[1] == None:
            continue
        t[0].val += t[1].val
        if t[0].left == None:
            t[0].left = t[1].left
        else:
            stack.append((t[0].left, t[1].left))
        if t[0].right == None:
            t[0].right = t[1].right
        else:
            stack.append((t[0].right, t[1].right))
    return t1


if __name__ == '__main__':
    tree_node = TreeNode()
    t1 = tree_node.create_binary_tree_array([1, 2, 3], 0)
    t2 = tree_node.create_binary_tree_array([1, 2, 3], 0)
    t = merge_trees2(t1, t2)
    t_arr = per_order_traversal(t)
    print(t_arr)
Пример #5
0
        root: 节点root
        p: 节点p
        q: 节点q
    Returns:
        最小公共祖先
    '''
    if not root:
        return root
    if not p or not q:
        return root
    left = lowest_common_ancestor(root.left, p, q)
    right = lowest_common_ancestor(root.right, p, q)
    if not left and not right:
        return None
    if left and right:
        return root
    if not left:
        return right
    else:
        return left


if __name__ == '__main__':
    nums = [3, 5, 1, 6, 2, 0, 8, None, None, 7, 4]
    tree_node = TreeNode()
    p = TreeNode(5)
    q = TreeNode(1)
    root = tree_node.create_binary_tree_array(nums)
    result = lowest_common_ancestor(root, p, q)
    print(result)
from tree.tree_node import TreeNode


class Solution:
    def tree_2_str(self, root: TreeNode) -> str:
        '''
            将树转成字符串
        Args:
            root: 二叉树
        Returns:
            字符串str
        '''
        if root == None:
            return ''
        left = str(self.tree_2_str(root.left))
        right = str(self.tree_2_str(root.right))
        left = '' if len(left) < 1 and len(right) < 1 else '(' + str(
            left) + ')'
        right = '' if len(right) < 1 else '(' + str(right) + ')'
        return str(root.val) + left + right


if __name__ == '__main__':
    nums = [1, 2, 3, 4]
    root = TreeNode.create_binary_tree_array(nums)
    solution = Solution()
    result = solution.tree_2_str(root)
    print(result)
    assert result == '1(2(4))(3)'