Binary Tree High-Frequency Problems (Non Tree-DP)
高频二叉树题型识别清单:BFS 层序、DFS 分解/遍历、常见易错点与例题链接
#resource / algorithm
#type / concept
#status / growing
#ds / tree
#algo / bfs
#algo / dfs
[!info] related notes 算法面试题型 MOC 二叉树基础
二叉树高频题型清单(非树形 DP)
面试里大部分二叉树题都能落到两种视角:
- 遍历思维:按某种顺序走一遍,沿途做事(常见:路径、统计、收集)
- 分解思维:假设左右子树的答案已知,合成当前答案(常见:高度、节点数、判定类)
如果你还没把术语/遍历模板吃透,先看:二叉树基础。
1) BFS:层序遍历与按层处理
- Two common patterns:
- Use a queue and store
(node, level). - Process per-level: capture
levelSizefirst, loop exactly that many nodes.
- Use a queue and store
- Reference implementation: 二叉树层序遍历
典型题(热题 100 常见):
- 二叉树层序遍历(102)
2) BFS 变体:锯齿形层序
- Per-level BFS, but alternate direction when writing into result.
- Implementation options:
- Reverse the level list on odd levels.
- Or write into array with index mapping.
- Reference implementation: 二叉树锯齿形层序遍历
3) BFS 变体:带编号的层序
- BFS with index numbering and per-level normalization.
- Reference implementation: 二叉树最大宽度
4) BFS 判定:完全二叉树
- BFS in order.
- Rules:
- A node has right child but no left child => not complete.
- Once a node is missing any child, all following nodes must be leaves.
- Reference implementation: 验证完全二叉树
5) 利用完全二叉树性质
- Use the property of complete trees:
- Compare the height of the leftmost path in right subtree.
- If it reaches the tree height, left subtree is perfect.
- Otherwise right subtree is perfect.
- Reference implementation: 完全二叉树节点个数
6) DFS 分解:最近公共祖先(LCA)
- Recursively return:
null(not found)porq(found)- current node (if both sides found)
典型题:二叉树最近公共祖先(236)
7) DFS 判定:结构/镜像/路径
这类题“遍历”很简单,但最容易写挂在空节点/叶子判断。
热题 100 常见: