把二叉树打印成多行 牛客网 剑指Offer

 

  • 题目描述
  • 从上到下按层打印二叉树,同一层结点从左至右输出。每一层输出一行
# 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):
        lt = []
        self.printTree(lt,pRoot,0)
        return lt
        
    def printTree(self,lt,pRoot,deep):
        if pRoot == None:
            return 
        while len(lt) < deep+1:
            lt.append([])
        l = lt[deep]
        l.append(pRoot.val)
        self.printTree(lt,pRoot.left,deep+1)
        self.printTree(lt,pRoot.right,deep+1)

 

相关文章:

  • 2021-12-09
  • 2022-12-23
  • 2021-10-29
  • 2022-12-23
  • 2021-10-16
  • 2021-12-16
  • 2022-12-23
  • 2021-08-07
猜你喜欢
  • 2021-10-15
  • 2022-02-04
  • 2022-12-23
  • 2022-12-23
  • 2021-12-17
  • 2021-06-20
  • 2022-12-23
相关资源
相似解决方案