首页 > 编程语言 > 详细

把二叉树打印成多行 -python

时间:2019-08-13 09:07:58      阅读:74      评论:0      收藏:0      [点我收藏+]

思路:跟上一篇的之字形打印思路类似,只不过不用隔层翻转打印

# -*- coding:utf-8 -*-
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
class Solution:
    # 返回二维列表[[1,2],[4,5]]
    def Print(self, pRoot):
        # write code here
        if not pRoot:
            return []
        curLayer = [pRoot]
        res = []
        while curLayer:
            nextLayer = []
            tmp = []
            for node in curLayer:
                tmp.append(node.val)
                if node.left:
                    nextLayer.append(node.left)
                if node.right:
                    nextLayer.append(node.right)
            curLayer = nextLayer
            res.append(tmp)
        return res

把二叉树打印成多行 -python

原文:https://www.cnblogs.com/dolisun/p/11343726.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!