_ Prove safety offer_ binary print binary tree from the top down

Binary tree binary tree from the top down _ Print

Description Title
print out from the top of each node of the binary tree, with the layer node from left to right printing.
Problem Analysis

# -*- coding:utf-8 -*-
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
class Solution:
    # 返回从上到下每个节点值列表,例:[1,2,3]
    def PrintFromTopToBottom(self, root):
        # write code here
        queue = []
        result = []
        if not root:
            return result
        queue.append(root)
        while queue:
            newnode = queue.pop(0)
            result.append(newnode.val)
            if newnode.left:
                queue.append(newnode.left)
            if newnode.right:
                queue.append(newnode.right)
        return result
Published 31 original articles · won praise 0 · Views 720

Guess you like

Origin blog.csdn.net/freedomUSTB/article/details/105128662