[Leetcode总结] 102.二叉树的层序遍历
生活随笔
收集整理的這篇文章主要介紹了
[Leetcode总结] 102.二叉树的层序遍历
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
給你一個二叉樹,請你返回其按?層序遍歷?得到的節點值。
示例:
二叉樹:[3,9,20,null,null,15,7],
返回其層序遍歷結果:
[[3],[9,20],[15,7] ] class Solution():def levelOrder(self,root:TreeNode)->List[List[int]]:res = []que = [root]if root == None:return reswhile que:tempList = []for i in range(len(que)):node = que.pop(0)tempList.append(node.val)if node.left:que.append(node.left)if node.right:que.append(node.right)res.append(tempList)return res總結
以上是生活随笔為你收集整理的[Leetcode总结] 102.二叉树的层序遍历的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 验证码之字符的特征提取
- 下一篇: [Leetcode总结] 104.二叉树