Leetcode原题链接:
二叉树的层序遍历
二叉树的层序遍历 II
二叉树的右视图
二叉树的层平均值
N 叉树的层序遍历
在每个树行中找最大值
填充每个节点的下一个右侧节点指针
填充每个节点的下一个右侧节点指针 II
二叉树的最大深度
二叉树的最小深度
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
if not root:
return []
queue = [root]
result = []
while queue:
tmp = []
for i in range(len(queue)):
cur = queue.pop(0)
tmp.append(cur.val)
if cur.left:
queue.append(cur.left)
if cur.right:
queue.append(cur.right)
result.append(tmp)
return result
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def levelOrderBottom(self, root: Optional[TreeNode]) -> List[List[int]]:
if not root:
return []
result = []
queue = [root]
while queue:
tmp = []
size = len(queue)
for i in range(size):
cur = queue.pop(0)
tmp.append(cur.val)
if cur.left:
queue.append(cur.left)
if cur.right:
queue.append(cur.right)
result.append(tmp)
return result[::-1]
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def rightSideView(self, root: Optional[TreeNode]) -> List[int]:
if not root:
return []
result = []
queue = [root]
while queue:
tmp = []
size = len(queue)
for i in range(size):
cur = queue.pop(0)
tmp.append(cur.val)
if cur.left:
queue.append(cur.left)
if cur.right:
queue.append(cur.right)
result.append(tmp)
realreturn = []
for res in result:
realreturn.append(res[-1])
return realreturn
size-1
,如果是再提取出来# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def rightSideView(self, root: Optional[TreeNode]) -> List[int]:
if not root:
return []
result = []
queue = [root]
while queue:
size = len(queue)
for i in range(size):
cur = queue.pop(0)
if i == size-1:
result.append(cur.val)
if cur.left:
queue.append(cur.left)
if cur.right:
queue.append(cur.right)
return result
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def averageOfLevels(self, root: Optional[TreeNode]) -> List[float]:
result = []
queue = [root]
while queue:
tmp = []
for i in range(len(queue)):
cur = queue.pop(0)
tmp.append(cur.val)
if cur.left:
queue.append(cur.left)
if cur.right:
queue.append(cur.right)
result.append(sum(tmp) / len(tmp))
return result
"""
# Definition for a Node.
class Node:
def __init__(self, val=None, children=None):
self.val = val
self.children = children
"""
class Solution:
def levelOrder(self, root: 'Node') -> List[List[int]]:
if not root:
return []
result = []
queue = [root]
while queue:
tmp = []
for i in range(len(queue)):
cur = queue.pop(0)
tmp.append(cur.val)
for child in cur.children:
queue.append(child)
result.append(tmp)
return result
children
是个列表,看完解析才恍然大悟
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def largestValues(self, root: Optional[TreeNode]) -> List[int]:
if not root:
return []
result = []
queue = [root]
while queue:
num_max = []
for i in range(len(queue)):
node = queue.pop(0)
if not num_max:
num_max.append(node.val)
else:
num_max[0] = self.retBig(node.val, num_max[0])
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
result.append(num_max[0])
return result
def retBig(self, a, b):
if a > b:
return a
else:
return b
num_max
初始化应该设为多少,解决方法取了个巧,不初始化,存在列表的第一个位置。。python
自带的有,不用自己定义下面是解决以上两问题的代码:
-inf
max()
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def largestValues(self, root: TreeNode) -> List[int]:
if not root:
return []
result = []
queue = collections.deque([root])
while queue:
level_size = len(queue)
max_val = float('-inf')
for _ in range(level_size):
node = queue.popleft()
max_val = max(max_val, node.val)
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
result.append(max_val)
return result
"""
# Definition for a Node.
class Node:
def __init__(self, val: int = 0, left: 'Node' = None, right: 'Node' = None, next: 'Node' = None):
self.val = val
self.left = left
self.right = right
self.next = next
"""
class Solution:
def connect(self, root: 'Optional[Node]') -> 'Optional[Node]':
if not root:
return root
queue = [root]
while queue:
size = len(queue)
for i in range(size):
node = queue.pop(0)
if i < size-1:
node.next = queue[0]
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
return root
"""
# Definition for a Node.
class Node:
def __init__(self, val: int = 0, left: 'Node' = None, right: 'Node' = None, next: 'Node' = None):
self.val = val
self.left = left
self.right = right
self.next = next
"""
class Solution:
def connect(self, root: 'Node') -> 'Node':
if not root:
return root
queue = [root]
while queue:
size = len(queue)
for i in range(size):
node = queue.pop(0)
if i < size-1:
node.next = queue[0]
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
return root
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
depth = 0
queue = [root]
while queue:
size = len(queue)
for i in range(size):
node = queue.pop(0)
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
depth += 1
return depth
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def minDepth(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
queue = [root]
min_depth = 1
while queue:
size = len(queue)
for i in range(size):
node = queue.pop(0)
if node.left == None and node.right == None:
return min_depth
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
min_depth += 1
return min_depth
部分内容参考代码随想录