leetcode Python 广度优先遍历打印二叉树


# Definition for a binary tree node.
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None


class Solution:
    def levelOrder(self, root):
        """
        :type root: TreeNode
        :rtype: List[List[int]]
        """
        res=[]
        if root is None:
            return res
        q=[root]
        while len(q)!=0:
            res.append([node.val for node in q])
            new_q=[]
            for node in q:
                if node.left:
                    new_q.append(node.left)
                if node.right:
                    new_q.append(node.right)
            q=new_q
            
        return res
                    

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-05-07
  • 2021-12-27
  • 2022-12-23
猜你喜欢
  • 2021-05-18
  • 2021-12-29
  • 2021-04-03
  • 2021-12-27
  • 2021-12-05
  • 2022-12-23
  • 2021-11-07
相关资源
相似解决方案