一篇文章教你如何用多种迭代写法实现二叉树遍历
作者:保护眼睛 时间:2023-12-23 04:03:29
思想
利用栈和队列都可以实现树的迭代遍历。递归的写法将这个遍历的过程交给系统的堆栈去实现了,所以思想都是一样的、无非就是插入值的时机不一样。利用栈的先进先出的特点,对于前序遍历、我们可以先将当前的值放进结果集中,表示的是根节点的值、然后将当前的节点加入到栈中、当前的节点等于自己的left、再次循环的时候、也会将left作为新的节点、直到节点为空、也就是走到了树的最左边、然后回退、也就是弹栈、、也可以认为回退的过程是从低向上的、具体就是让当前的节点等于栈弹出的right、继续重复上面的过程,也就实现了树的前序遍历、也就是bfs.后续遍历、中序遍历思想也是类似的。
实现
public List<Integer> preorderTraversal1(TreeNode root) {
List<Integer> res = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
while (!stack.isEmpty() || root != null) {
while (root != null) {
res.add(root.val);
stack.add(root);
root = root.left;
}
TreeNode cur = stack.pop();
root = cur.right;
}
return res;
}
public List<Integer> preorderTraversal2(TreeNode root) {
List<Integer> res = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
while (!stack.isEmpty() || root != null) {
if (root != null) {
res.add(root.val);
stack.add(root);
root = root.left;
} else {
TreeNode cur = stack.pop();
root = cur.right;
}
}
return res;
}
public List<Integer> preorderTraversal3(TreeNode root) {
List<Integer> res = new ArrayList<>();
if (root == null) return res;
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while (!stack.isEmpty()) {
TreeNode cur = stack.pop();
res.add(cur.val);
if (cur.right != null) {
stack.push(cur.right);
}
if (cur.left != null) {
stack.push(cur.left);
}
}
return res;
}
public List<Integer> preorderTraversal4(TreeNode root) {
List<Integer> res = new ArrayList<>();
if (root == null) {
return res;
}
LinkedList<TreeNode> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
root = queue.poll();
res.add(root.val);
if (root.right != null) {
queue.addFirst(root.right);
}
if (root.left != null) {
root = root.left;
while (root != null) {
res.add(root.val);
if (root.right != null) {
queue.addFirst(root.right);
}
root = root.left;
}
}
}
return res;
}
public List<Integer> inorderTraversal1(TreeNode root) {
List<Integer> res = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
while (root != null || !stack.isEmpty()) {
if (root != null) {
stack.add(root);
root = root.left;
} else {
TreeNode cur = stack.pop();
res.add(cur.val);
root = cur.right;
}
}
return res;
}
public List<Integer> inorderTraversal2(TreeNode root) {
List<Integer> res = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
while (root != null || !stack.isEmpty()) {
while (root != null) {
stack.add(root);
root = root.left;
}
TreeNode cur = stack.pop();
res.add(cur.val);
root = cur.right;
}
return res;
}
public List<Integer> postorderTraversal1(TreeNode root) {
List<Integer> res = new ArrayList<>();
if (root == null) return res;
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while (!stack.isEmpty()) {
TreeNode cur = stack.pop();
res.add(cur.val);
if (cur.left != null) {
stack.push(cur.left);
}
if (cur.right != null) {
stack.push(cur.right);
}
}
Collections.reverse(res);
return res;
}
public List<Integer> postorderTraversal2(TreeNode root) {
List<Integer> res = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
while (!stack.isEmpty()) {
while (root != null) {
res.add(root.val);
stack.push(root);
root = root.right;
}
TreeNode cur = stack.pop();
root = cur.left;
}
Collections.reverse(res);
return res;
}
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> ret = new ArrayList<>();
if(root == null)return ret;
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()){
int size = queue.size();
List<Integer> list = new ArrayList<>();
while(size!=0){
TreeNode cur = queue.poll();
list.add(cur.val);
if(cur.left!=null){
queue.offer(cur.left);
}
if(cur.right!= null){
queue.offer(cur.right);
}
size --;
}
ret.add(list);
}
return ret;
}
来源:https://blog.csdn.net/qq_45859087/article/details/119141358
标签:迭代,二叉树遍历
0
投稿
猜你喜欢
C#读取写入文件的3种方式示例代码
2022-09-15 06:57:38
Java 创建两个线程模拟对话并交替输出实现解析
2022-01-10 02:37:02
如何使用C#在PDF文件添加图片印章
2021-06-26 05:34:51
android view转Bitmap生成截图的方法
2022-11-07 13:41:53
JavaSwing FlowLayout 流式布局的实现
2023-10-02 03:59:41
C#中ValueTuple的原理详解
2023-10-26 06:40:34
Android自定义控件实现边缘凹凸的卡劵效果
2022-10-01 01:20:51
浅谈Java中几种常见的比较器的实现方法
2022-04-08 19:04:36
Android实现蒙板效果
2021-08-30 16:06:04
Java接口幂等性设计原理解析
2022-12-22 12:27:01
C++ OpenCV单峰三角阈值法Thresh_Unimodal详解
2022-06-09 14:36:46
Java中数组在内存中存放原理的讲解
2022-12-10 03:48:35
详解SpringMVC使用MultipartFile实现文件的上传
2023-03-28 00:48:18
Java之Rsync并发迁移数据并校验详解
2023-07-17 23:01:22
学习Winform文本类控件(Label、Button、TextBox)
2022-12-29 09:00:48
Android自定义VIew实现卫星菜单效果浅析
2022-09-23 22:44:43
为什么Spring官方推荐的@Transational还能导致生产事故
2022-02-13 15:54:37
Spring如何将bean添加到容器中
2021-08-07 08:48:06
浅谈一下SpringCloud中Hystrix服务熔断和降级原理
2021-10-02 08:46:41
Java集合之Map接口的实现类精解
2023-10-07 15:10:37