Python print binary tree from the top down

Downward from the print out of each node of the binary tree, with the layer node from left to right printing.

class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Solution:
    def PrintFromTopToBottom(self,root):
        if root == None:
            return []
        support = [root]
        ret =[]
        while support:
            tmpNode = support[0]
            ret.append(tmpNode.val)
            if tmpNode.left:
                support.append(tmpNode.left)
            if tmpNode.right:
                support.append(tmpNode.right)
            del support[0]
        return  ret
Published 135 original articles · won praise 121 · Views 4862

Guess you like

Origin blog.csdn.net/weixin_44208324/article/details/105314075