这一节几道题目都是应用层次遍历的基本思路解决问题,当然也有不用层次遍历思想的解法,我们也会介绍一下。层次遍历可以同时处理二叉树每一层结点,许多问题迎刃而解。
(三)二叉树层次遍历
103. Binary Tree Zigzag Level Order Traversal
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).
题目解析:
二叉树蛇形遍历,层次遍历的最简单变种,加一个标记,偶数行遍历的结果逆转即可,切记结点访问顺序不可不变的,改变一下结果即可。
class Solution:
def zigzagLevelOrder(self, root: TreeNode) -> List[List[int]]:
if not root:
return []
stack = [root]
f = False
res = []
while stack:
vals = []
next_stack = []
for p in stack:
vals.append(p.val)
if p.left:
next_stack.append(p.left)
if p.right:
next_stack.append(p.right)
if f:
vals.reverse()
res.append(vals)
stack = next_stack
f = not f
return res
117. Populating Next Right Pointers in Each Node II
Given a binary tree
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL
.
Initially, all next pointers are set to NULL
.
题目解析:
方法一,层次遍历的基本思想,在每层的结点遍历过程中,将next指针加上,简洁易懂;
方法二,按题目要求限定了空间复杂度,动态应用上一层next指针的结果,将下一层依次连接。(递归的代码也是这个思路)
def connect(self, root: 'Node') -> 'Node':
if not root:
return None
stack = [root]
while stack:
next_stack = []
pre = None
for p in stack:
if p.left:
next_stack.append(p.left)
if p.right:
next_stack.append(p.right)
if pre:
pre.next = p
pre = p
stack = next_stack
return root
class Solution:
def connect(self, root: 'Node') -> 'Node':
if not root:
return None
parent = root
next_level_parent = root.left or root.right
while parent:
if parent.left and parent.right:
parent.left.next = parent.right
right_most_child = parent.right or parent.left
if right_most_child:
right_most_child.next = self.find_next_left_most_child(parent.next)
if parent.next:
parent = parent.next
else:
parent = next_level_parent
#update next_level_parent
if parent:
next_left_most = self.find_next_left_most_child(parent)
next_level_parent = next_left_most
return root
def find_next_left_most_child(self, start_parent):
next_ = None
while not next_ and start_parent:
next_ = start_parent.left or start_parent.right
start_parent = start_parent.next
return next_
513. Find Bottom Left Tree Value
Given a binary tree, find the leftmost value in the last row of the tree.
Example 1:
Input:
2
/ \
1 3
Output:
1
题目解析:
方法一:层次遍历,保留最后一层,取第一个结点值即可;
方法二:层次遍历,但是结点从右向左,所以我们保留最后一个结点即可。(方法二的空间复杂度较高,可以应用其思想,遍历时结点出队;刷题平台不支持deque,用pop(0)可能会慢。理解思路就好)
class Solution:
def findBottomLeftValue(self, root: TreeNode) -> int:
if not root:
return
stack = [root]
while stack:
next_stack = []
for p in stack:
if p.left:
next_stack.append(p.left)
if p.right:
next_stack.append(p.right)
if not next_stack:
break
stack = next_stack
minVal = stack[0].val
return minVal
class Solution:
def findBottomLeftValue(self, root: TreeNode) -> int:
queue = []
queue.append(root)
for node in queue:
if node.right:
queue.append(node.right)
if node.left:
queue.append(node.left)
return node.val
515. Find Largest Value in Each Tree Row
You need to find the largest value in each row of a binary tree.
题目解析:
属于直接应用,一点弯儿都不绕。可以注意一下 float("-inf")的用法。
class Solution:
def largestValues(self, root: TreeNode) -> List[int]:
if not root:
return []
stack = [root]
res = []
while stack:
next_stack = []
maxVal = float("-inf")
for p in stack:
maxVal = max(maxVal, p.val)
if p.left:
next_stack.append(p.left)
if p.right:
next_stack.append(p.right)
res.append(maxVal)
stack = next_stack
return res
这一节的内容不算难,层次遍历的相关应用,往往比较直接,层次遍历的写法也比较简单,总之这部分没有太难的题目。leetcode上还有几道相关题目,就不再一一列出了。
下一节是二叉树中的重头戏,二叉树的路径问题~