题目链接:102. 二叉树的层序遍历 - 力扣(Leetcode)107. 二叉树的层序遍历 II - 力扣(Leetcode)199. 二叉树的右视图 - 力扣(Leetcode)226. 翻转二叉树 - 力扣(Leetcode)101. 对称二叉树 - 力扣(Leetcode)
102 二叉树的层序遍历
给你二叉树的根节点 root
,返回其节点值的 层序遍历 。 (即逐层地,从左到右访问所有节点)。
输入:root = [3,9,20,null,null,15,7] 输出:[[3],[9,20],[15,7]]
层序遍历,从上往下,从左往右,一层层的遍历。
疑问:
怎么区分每一层子树?
参考代码——递归法
class Solution:
def levelOrder(self, root: TreeNode) -> List[List[int]]:
res = []
def helper(root, depth):
if not root: return []
if len(res) == depth: res.append([]) # start the current depth
res[depth].append(root.val) # fulfil the current depth
if root.left: helper(root.left, depth + 1) # process child nodes for the next depth
if root.right: helper(root.right, depth + 1)
helper(root, 0)
return res
先把每一层的root加进去,再把其每一层的左子点加进去,同理右子点。
参考代码——迭代法
class Solution:
def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
results = []
if not root:
return results
from collections import deque
que = deque([root])
while que:
size = len(que) //对当前队列长度进行快照,因为中间会有两层子树混合一起的时候
result = []
for _ in range(size):
cur = que.popleft()
result.append(cur.val)
if cur.left:
que.append(cur.left)
if cur.right:
que.append(cur.right)
results.append(result)
return results
107 二叉树的层序遍历II
给你二叉树的根节点 root
,返回其节点值 自底向上的层序遍历 。 (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历)
输入:root = [3,9,20,null,null,15,7] 输出:[[15,7],[9,20],[3]]
正常思路:认真的自底向上遍历
摸鱼思路:正常层序遍历后reverse()
199 二叉树的右视图
给定一个二叉树的 根节点 root
,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。
输入: [1,2,3,null,5,null,4] 输出: [1,3,4]
放进栈/队列里的不一定是要输出的,只有要返回的结果才需要在pop之后append进去,再返回。
class Solution:
def rightSideView(self, root: Optional[TreeNode]) -> List[int]:
if not root:
return []
from collections import deque
queue = deque([root])
out_list = []
while queue:
node = queue[-1]
out_list.append(node.val)
for _ in range(len(queue)):
node = queue.popleft()
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
return out_list
226 翻转二叉树
给你一棵二叉树的根节点 root
,翻转这棵二叉树,并返回其根节点。
输入:root = [4,2,7,1,3,6,9] 输出:[4,7,2,9,6,3,1]
遍历的时候翻转同一层的子节点即可,但要注意遍历方式。
前序和后序都可以,但是用中序的话会翻转两次,所以不可以用中序。
class Solution:
def invertTree(self, root: TreeNode) -> TreeNode:
if not root:
return None
root.left, root.right = root.right, root.left #中
self.invertTree(root.left) #左
self.invertTree(root.right) #右
return root
101 对称二叉树
给你一个二叉树的根节点 root
, 检查它是否轴对称。
输入:root = [1,2,2,3,4,4,3] 输出:true
初步思路:需要比较左右子树是否为对称翻转,因此把左子树翻转后对比右子树看是否一致。也就是说,需要遍历两边子树。
遍历方式:只能使用后序遍历。因为由题意可知,我需要把左右子点的信息返回给根节点,也就是上一层的节点。遍历也需遵循对称。
小心得:
前中后序遍历的合理运用,代表着二叉树节点处理的顺序和需要的信息类型。本题使用后序遍历,是需要我们收集子节点的信息,向上一层返回这些信息,如遇同类型的题目都可考虑后序遍历。
疑问:为什么要先排除掉所有空节点和非空节点但数值不同的情况,是否可以在非空节点且数值相同的情况下继续进行,不然就return false?
参考代码——递归法
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