-
中序遍历顺序:[4 2 5 1 3 6]
-
后序遍历顺序:[4 5 2 6 3 1]
层次遍历使用 BFS 实现,利用的就是 BFS 一层一层遍历的特性;而前序、中序、后序遍历利用了 DFS 实现。
前序、中序、后序遍只是在对节点访问的顺序有一点不同,其它都相同。
递归实现前序,中序,后序遍历
① 前序
1void dfs(TreeNode root) {
2 visit(root);
3 dfs(root.left);
4 dfs(root.right);
5}
② 中序
1void dfs(TreeNode root) {
2 dfs(root.left);
3 visit(root);
4 dfs(root.right);
5}
③ 后序
1void dfs(TreeNode root) {
2 dfs(root.left);
3 dfs(root.right);
4 visit(root);
5}
1. 非递归实现二叉树的前序遍历
144. Binary Tree Preorder Traversal (Medium)
Leetcode / 力扣:https://leetcode-cn.com/problems/binary-tree-preorder-traversal/description/
1class Solution {
2 //迭代
3 public List preorderTraversal(TreeNode root) {
4 List res = new ArrayList();
5 Stack stack = new Stack();
6 while(root!=null || !stack.empty()){
7 while(root!=null){
8 res.add(root.val); //先将节点加入结果队列
9 stack.push(root); //不断将该节点左子树入栈
10 root = root.left;
11 }
12 root = stack.pop(); //栈顶节点出栈
13 root = root.right; //转向该节点右子树的左子树(下一个循环)
14 }
15 return res;
16 }
17
18}
2. 非递归实现二叉树的中序遍历
94. Binary Tree Inorder Traversal (Medium)
Leetcode / 力扣:https://leetcode-cn.com/problems/binary-tree-inorder-traversal/description/
1class Solution {
2 //迭代
3 public List preorderTraversal(TreeNode root) {
4 List res = new ArrayList();
5 Stack stack = new Stack();
6 while(root!=null || !stack.empty()){
7 while(root!=null){
8 stack.push(root); //不断将该节点左子树入栈
9 root = root.left;
10 }
11 root = stack.pop(); //栈顶节点出栈
12 res.add(root.val); //将节点加入结果队列
13 root = root.right; //转向该节点右子树的左子树(下一个循环)
14 }
15 return res;
16 }
17}
3. 非递归实现二叉树的后序遍历
145. Binary Tree Postorder Traversal (Medium)
Leetcode / 力扣:https://leetcode-cn.com/problems/binary-tree-postorder-traversal/description/
前序遍历为 root -> left -> right,后序遍历为 left -> right -> root。可以修改前序遍历成为 root -> right -> left,那么这个顺序就和后序遍历正好相反。
1//修改前序遍历代码中,节点写入结果链表的代码:将插入队尾修改为插入队首
2//修改前序遍历代码中,每次先查看左节点再查看右节点的逻辑:变为先查看右节点再查看左节点
3class Solution {
4 public List postorderTraversal(TreeNode root) {
5 LinkedList res = new LinkedList();
6 Stack stack = new Stack<>();
7 TreeNode pre = null;
8 while(root!=null || !stack.empty()){
9 while(root!=null){
10 res.addFirst(root.val); //插入队首
11 stack.push(root);
12 root = root.right; //先右后左
13 }
14 root = stack.pop();
15 root = root.left;
16 }
17 return res;
18 }
19}
4. 非递归实现二叉树的层序遍历
leetcode:https://leetcode.com/problems/binary-tree-level-order-traversal-ii/description/?spm=a2c4e.10696291.0.0.5e5719a42W3zNP
1class Solution {
2 public List<List> levelOrderBottom(TreeNode root) {
3 List<List> res = new LinkedList<>();
4 Queue queue = new LinkedList<>();
5 if(root == null)
6 return res;
7 queue.add(root);
8 while(!queue.isEmpty()){
9 int count = queue.size();
10 List temp = new LinkedList<>();
11 for(int i=0; i<count; i++){
12 TreeNode node = queue.poll();
13 temp.add(node.val);
14 if(node.left != null)
15 queue.add(node.left);
16 if(node.right != null)
17 queue.add(node.right);
18 }
19 // 每次都添加到第一个位置
20 res.add(0, temp);
21 }
22 return res;
23 }
24}
最后
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。
因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
4xwc2g1-1715802882310)]
[外链图片转存中…(img-HuwhULHk-1715802882311)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!