面试题32:从上到下打印二叉树

面试题32:从上到下打印二叉树

# -*- 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
        if root == None:
            return []
        # 构造一个辅助list存访节点
        support = [root]
        # 返回的数字
        ret = []
        
        while support:
            tempnode = support[0]
            ret.append(tempnode.val)
            
            if tempnode.left != None:
                support.append(tempnode.left)
            if tempnode.right != None:
                support.append(tempnode.right)
            del support[0]
        return ret

  

上一篇:Java学习第30天:Huffman 编码 (编码与解码)


下一篇:A*算法求解八数码问题