题解 | #求二叉树的层序遍历#

求二叉树的层序遍历

https://www.nowcoder.com/practice/04a5560e43e24e9db4595865dc9c63a3

    class Solution:
def levelOrder(self , root: TreeNode) -> List[List[int]]:
# write code here
a=0
b=0
list_temp1=[]#存值
list_temp2=[]#存结点
List=[]
	now=root
    while now!=None:
        list_temp1.append(now.val)
                                
        if now.left!=None:
            list_temp2.append(now.left) 
            b+=1 

        if now.right!=None:
            list_temp2.append(now.right) 
            b+=1

        if a==0:
            List.append(list_temp1[:])
            list_temp1.clear()
            a=b
            b=0

        if list_temp2:
            now=list_temp2.pop(0)
            a-=1
        else:
            break

    return List

全部评论

相关推荐

LXXXXd:有点杂,想搞自动化的话没必要把法律的经历写上去
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务