二叉树的层序遍历 226.翻转二叉树 101.对称二叉树
今天开始二叉树的层序遍历,反转二叉树、对称二叉树。
1.层序遍历
层序遍历一个二叉树。就是从左到右一层一层的去遍历二叉树。
需要借用一个辅助数据结构即队列来实现,队列先进先出,符合一层一层遍历的逻辑,而用栈先进后出适合模拟深度优先遍历也就是递归的逻辑。
而这种层序遍历方式就是图论中的广度优先遍历,只不过此时应用在二叉树上。
102. 二叉树的层序遍历
给你二叉树的根节点 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 levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
result = []
if not root:
return result
from collections import deque
que = deque([root])
while que:
res = []
size = len(que)
for _ in range(size):
cur = que.popleft()
res.append(cur.val)
if cur.left:
que.append(cur.left)
if cur.right:
que.append(cur.right)
result.append(res)
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 levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
res = []
def helper(root,depth):
if not root:return []
if len(res) == depth:res.append([])
res[depth].append(root.val)
if root.left:helper(root.left,depth+1)
if root.right:helper(root.right,depth+1)
helper(root,0)
return res
226. 翻转二叉树
给你一棵二叉树的根节点 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
root.left ,root.right = root.right ,root.left
self.invertTree(root.left)
self.invertTree(root.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
tmp = []
tmp.append(root)
while tmp:
cur = tmp.pop()
cur.left , cur.right = cur.right , cur.left
if cur.right:
tmp.append(cur.right)
if cur.left:
tmp.append(cur.left)
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]:
from collections import deque
que = deque()
if root:
que.append(root)
else:
return None
while que:
size = len(que)
for _ in range(size):
cur = que.popleft()
cur.left , cur.right = cur.right , cur.left
if cur.left:
que.append(cur.left)
if cur.right:
que.append(cur.right)
return root
101.对称二叉树
给你一个二叉树的根节点 root , 检查它是否轴对称。
递归法:
class Solution:
def isSymmetric(self, root: TreeNode) -> bool:
if not root:
return True
return self.compare(root.left, root.right)
def compare(self, left, right):
#首先排除空节点的情况
if left == None and right != None: return False
elif left != None and right == None: return False
elif left == None and right == None: return True
elif left.val != right.val: return False
outside = self.compare(left.left, right.right) #左子树:左、 右子树:右
inside = self.compare(left.right, right.left) #左子树:右、 右子树:左
isSame = outside and inside #左子树:中、 右子树:中 (逻辑处理)
return isSame
今天题目虽然不难,但是要真正思路清晰,一次写出来,还是不容易啊,要掌握递归、迭代法,什么时候用栈,什么时候用队列去存node,访问节点顺序、处理节点顺序是否一致。