版权声明
- LeetCode 系列笔记来源于 LeetCode 题库1,在个人思考的基础之上博采众长,受益匪浅;故今记此文,感怀于心,更多题解及程序,参见 Github2;
- 该系列笔记不以盈利为目的,仅用于个人学习、课后复习及交流讨论;
- 如有侵权,请与本人联系(hqpan@foxmail.com),经核实后即刻删除;
- 本文采用 署名-非商业性使用-禁止演绎 4.0 国际 (CC BY-NC-ND 4.0) 协议发布;
1. LeetCode 102
- 递归:
- 时间复杂度:;
- 空间复杂度:;
- 迭代:
- 时间复杂度:;
- 空间复杂度:;
2. 带有层数参数的递归
- 解题思路:
- 为将各节点的值分层存入二维 List 中,应使用 BFS 进行层次遍历;
- 难点:遍历过程中需要判断某个节点位于哪一层;
- 解决方案:执行广度优先搜索的函数应具备两个形参,即节点和层数;
// Approach 1: Recursion
class Solution {
List<List<Integer>> list = new ArrayList<List<Integer>>();
public List<List<Integer>> levelOrder(TreeNode root) {
if (root == null)
return list;
bfs(root, 0);
return list;
}
private void bfs(TreeNode node, int floor)
{
if (list.size() == floor)
list.add(new ArrayList<Integer>());
list.get(floor).add(node.val);
if (node.left != null)
bfs(node.left, floor + 1);
if (node.right != null)
bfs(node.right, floor + 1);
}
}
3. 区分层数的迭代
- 解题思路:同递归;
- 难点:遍历过程中需要判断某个节点位于哪一层;
- 解决方案:设定内循环的循环次数为队列的大小,处理当前队列中的所有节点,即完成当前层的访问;
// Approach 2: Iteration
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> list = new ArrayList<List<Integer>>(); // take notes
if (root == null)
return list;
Queue<TreeNode> queue = new LinkedList<>(); // take notes
queue.add(root);
while (!queue.isEmpty())
{
ArrayList<Integer> temp = new ArrayList<>();
for (int i = queue.size(); i > 0; i--)
{
TreeNode node = queue.poll();
temp.add(node.val);
if (node.left != null)
queue.add(node.left);
if (node.right != null)
queue.add(node.right);
}
list.add(temp);
}
return list;
}
}
References
来源:CSDN
作者:Andrew*
链接:https://blog.csdn.net/Maximize1/article/details/103490740