一、二叉树的层次遍历:
1.遍历步骤:从左到右一层一层的去遍历二叉树
2.使用队列
二、刷题:
1.leetcode题目102 二叉树的层序遍历(medium):
题目描述:102. 二叉树的层序遍历 - 力扣(LeetCode)
解决:
# 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 []
res = []
queue = collections.deque([root])
while queue:
level = []
for _ in range(len(queue)):
cur = queue.popleft()
level.append(cur.val)
if cur.left:
queue.append(cur.left)
if cur.right:
queue.append(cur.right)
res.append(level)
return res
2.leetcode题目107 二叉树的层序遍历(medium):
题目描述:107. 二叉树的层序遍历 II - 力扣(LeetCode)
解决:
# 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]]:
res = []
queue = collections.deque([root])
if root is None:
return []
while queue:
level = []
for _ in range(len(queue)):
cur = queue.popleft()
level.append(cur.val)
if cur.left:
queue.append(cur.left)
if cur.right:
queue.append(cur.right)
res.append(level)
return res[::-1]
3.leetcode题目199 二叉树的右视图(medium):
题目描述:199. 二叉树的右视图 - 力扣(LeetCode)
解决:
# 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]:
res = []
queue = collections.deque([root])
if root is None:
return []
while queue:
level = []
for _ in range(len(queue)):
cur = queue.popleft()
level.append(cur.val)
if cur.right:
queue.append(cur.right)
if cur.left:
queue.append(cur.left)
res.append(level[0])
return res
4.leetcode题目 二叉树的层平均值(easy):
题目描述:637. 二叉树的层平均值 - 力扣(LeetCode)
解决:
# 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]:
res = []
queue = collections.deque([root])
if root is None:
return []
while queue:
level_sum = 0
l = len(queue)
for _ in range(len(queue)):
cur = queue.popleft()
level_sum += cur.val
if cur.left:
queue.append(cur.left)
if cur.right:
queue.append(cur.right)
res.append(level_sum/l)
return res
5. leetcode题目429 N叉树的层序遍历(medium):
题目描述:429. N 叉树的层序遍历 - 力扣(LeetCode)
解决:
"""
# 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]]:
res = []
queue = collections.deque([root])
if root is None:
return []
while queue:
level = []
for _ in range(len(queue)):
cur = queue.popleft()
level.append(cur.val)
if cur.children:
for child in cur.children:
queue.append(child)
res.append(level)
return res
6.leetcode 题目 515 在每个树行中找最大值(medium):
题目描述:515. 在每个树行中找最大值 - 力扣(LeetCode)
解决:
# 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]:
res = []
queue = collections.deque([root])
if root is None:
return []
while queue:
max_l = -10**10
for _ in range(len(queue)):
cur = queue.popleft()
max_l = max(max_l,cur.val)
if cur.left:
queue.append(cur.left)
if cur.right:
queue.append(cur.right)
res.append(max_l)
return res
7.leetcode题目 116 填充每一个节点的下一个右侧节点指针(medium):
题目描述:116. 填充每个节点的下一个右侧节点指针 - 力扣(LeetCode)
解决:
"""
# 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]':
queue = collections.deque([root])
if not root:
return root
while queue:
l = len(queue)
for i in range(l):
cur = queue.popleft()
if i < l-1:
cur.next = queue[0]
if cur.left:
queue.append(cur.left)
if cur.right:
queue.append(cur.right)
return root
8.leetcode题目 117 填充每一个节点的下一个右侧节点指针II(medium):
题目描述:117. 填充每个节点的下一个右侧节点指针 II - 力扣(LeetCode)
解决:
"""
# 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':
queue = collections.deque([root])
if not root:
return root
while queue:
l = len(queue)
for i in range(l):
cur = queue.popleft()
if i < l-1:
cur.next = queue[0]
if cur.left:
queue.append(cur.left)
if cur.right:
queue.append(cur.right)
return root
9.leetcode题目 104 二叉树的最大深度(easy):
题目描述:104. 二叉树的最大深度 - 力扣(LeetCode)
解决:
①BFS:
# 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:
depth = 0
queue = collections.deque([root])
if not root:
return depth
while queue:
depth += 1
for _ in range(len(queue)):
cur = queue.popleft()
if cur.left:
queue.append(cur.left)
if cur.right:
queue.append(cur.right)
return depth
②DFS:
# 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
else:
left_height = self.maxDepth(root.left)
right_height = self.maxDepth(root.right)
return max(left_height,right_height) + 1
10. leetcode题目111 二叉树的最小深度(easy):
注意:只有当左右孩子都为空的时候,才说明遍历的最低点了。如果其中一个孩子为空则不是最低点。
题目描述:111. 二叉树的最小深度 - 力扣(LeetCode)
解决:
①DFS:
# 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
if not root.left and not root.right:
return 1
min_depth = 10**9
if root.left:
min_depth = min(self.minDepth(root.left),min_depth)
if root.right:
min_depth = min(self.minDepth(root.right),min_depth)
return min_depth + 1
②BFS:
# 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:
queue = collections.deque([root])
min_depth = 0
if not root:
return 0
if not root.left and not root.right:
return 1
while queue:
min_depth += 1
for _ in range(len(queue)):
cur = queue.popleft()
if not cur.left and not cur.right:
return min_depth
if cur.left:
queue.append(cur.left)
if cur.right:
queue.append(cur.right)
return min_depth
11.leetcode题目 222 完全二叉树的节点个数(easy):
题目描述:222. 完全二叉树的节点个数 - 力扣(LeetCode)
解决:
①BFS:
# 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 countNodes(self, root: Optional[TreeNode]) -> int:
num = 0
queue = collections.deque([root])
if not root:
return 0
while queue:
for _ in range(len(queue)):
cur = queue.popleft()
num += 1
if cur.left:
queue.append(cur.left)
if cur.right:
queue.append(cur.right)
return num
②DFS:
# 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 countNodes(self, root: Optional[TreeNode]) -> int:
num_left = 0
if not root:
return 0
else:
num_left = self.countNodes(root.left)
num_right = self.countNodes(root.right)
return num_left+num_right+1
12.leetcode题目 226 翻转二叉树(easy):
题目描述:
解决:
①迭代法
# 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 invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
if not root:
return None
stack = [root]
while stack:
node = stack.pop()
node.left,node.right = node.right,node.left
if node.left:
stack.append(node.left)
if node.right:
stack.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 invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
if not root:
return None
node = root
node.left,node.right = node.right,node.left
node.left = self.invertTree(node.left)
node.right = self.invertTree(node.right)
return node