剑指offer-把二叉树打印成多行(python)

# -*- 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 []
        res=[]
        tmp=[pRoot]
        tmp1=[]
        mid=[]
        while tmp:
            for i in tmp:
                mid.append(i.val)
                if i.left:
                    tmp1.append(i.left)
                if i.right:
                    tmp1.append(i.right)   
            res.append(mid)
            tmp=tmp1
            tmp1=[]
            mid=[]
        return res

猜你喜欢

转载自blog.csdn.net/qq_42738654/article/details/104617756