You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# 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: TreeNode) -> List[List[int]]:
if not root:
return None
queue = []
queue.insert(0, root)
res = []
level = 0
while queue:
length = len(queue)
res.append([])
for _ in range(length):
cur = queue.pop()
res[level].append(cur.val)
if cur.left:
queue.insert(0,cur.left)
if cur.right:
queue.insert(0,cur.right)
level+=1
return res
给定一个二叉树,返回其按层次遍历的节点值。 (即逐层地,从左到右访问所有节点)。
解法:用BFS的思想
The text was updated successfully, but these errors were encountered: