当前位置: 首页 > news >正文

java二叉树前中后序遍历

  • 代码随想录解题思路
  • 🆒力扣前序题目
  • 🆒力扣中序题目
  • 🆒力扣后序题目

递归遍历

// 前序遍历
class Solution {public List<Integer> preorderTraversal(TreeNode root) {List<Integer> res = new ArrayList<>();preorder(root,res);return res;}public void preorder(TreeNode root, List<Integer> res){if(root==null) return ;res.add(root.val);preorder(root.left,res);preorder(root.right,res);}
}// 中序遍历
class Solution {public List<Integer> inorderTraversal(TreeNode root) {List<Integer> res=new ArrayList<>();inorder(root,res);return res;}public void inorder(TreeNode root,List<Integer> res){if(root==null) return ;inorder(root.left,res);res.add(root.val);inorder(root.right,res);}
}// 后序遍历
class Solution {public List<Integer> postorderTraversal(TreeNode root) {List<Integer> res = new ArrayList<>();postorder(root, res);return res;}public void postorder(TreeNode root, List<Integer> res) {if (root == null)return;postorder(root.left,res);postorder(root.right,res);res.add(root.val);}
}

🆘二叉树的统一迭代遍历

💡一个大模板,前中后序只需要改变几句代码的顺序即可

代码随想录思路

package com.tree;import jdk.nashorn.internal.ir.SplitReturn;import java.util.*;class TreeNode {int val;TreeNode left;TreeNode right;TreeNode() {}TreeNode(int val) {this.val = val;}TreeNode(int val, TreeNode left, TreeNode right) {this.val = val;this.left = left;this.right = right;}
}class demo12_BinaryTreeTraversal {/*** 二叉树的非递归前序遍历** @param root* @return 前序遍历的节点列表*/public static List<Integer> preorderTraversal(TreeNode root) {List<Integer> res = new ArrayList<>();Stack<TreeNode> st = new Stack<>();if (root == null) {return res;} else st.push(root);while (!st.isEmpty()) {TreeNode node = st.peek();if (node != null) {st.pop();if (node.right != null) st.push(node.right);if (node.left != null) st.push(node.left);st.push(node);st.push(null);} else {st.pop();node = st.pop();res.add(node.val);}}return res;}/*** 二叉树的非递归中序遍历** @param root* @return 中序遍历的节点列表*/public static List<Integer> inorderTraversal(TreeNode root) {List<Integer> res = new ArrayList<>();Stack<TreeNode> st = new Stack<>();if (root == null) {return res;} else st.push(root);while (!st.empty()) {TreeNode cur = st.peek();if (cur != null) {st.pop();//中序遍历:左根右,so入栈顺序是右根左,在根入栈之后记得入栈一个null节点标记if (cur.right != null) st.push(cur.right);st.push(cur);st.push(null);if (cur.left != null) st.push(cur.left);} else {st.pop();cur = st.pop();res.add(cur.val);}}return res;}/*** 二叉树的非递归后序遍历** @param root* @return 后序遍历的节点列表*/public static List<Integer> postorderTraversal(TreeNode root) {List<Integer> res = new ArrayList<>();if (root == null) {return res;}Stack<TreeNode> st = new Stack<>();st.push(root);while (!st.empty()) {TreeNode point = st.peek();if (point != null) {//出栈左右根,入栈根右左st.pop();st.push(point);st.push(null);if (point.right != null) st.push(point.right);if (point.left != null) st.push(point.left);} else {st.pop();point = st.pop();res.add(point.val);}}return res;}public static void main(String[] args) {// 创建二叉树 [1,null,2,3]TreeNode root = new TreeNode(1);root.right = new TreeNode(2);root.right.left = new TreeNode(3);System.out.println(preorderTraversal(root));System.out.println(postorderTraversal(root));System.out.println(inorderTraversal(root));}
}

迭代遍历

import jdk.nashorn.internal.ir.SplitReturn;import java.util.*;class TreeNode {int val;TreeNode left;TreeNode right;TreeNode() {}TreeNode(int val) {this.val = val;}TreeNode(int val, TreeNode left, TreeNode right) {this.val = val;this.left = left;this.right = right;}
}class demo12_BinaryTreeTraversal {/*** 二叉树的非递归中序遍历** @param root* @return 中序遍历的节点列表*/public static List<Integer> inorderTraversal(TreeNode root) {List<Integer> res = new ArrayList<>();Stack<TreeNode> st = new Stack<>();TreeNode cur = root; // 设定一个指针while (cur != null || !st.isEmpty()) {if (cur != null) {st.push(cur);cur = cur.left;} else {cur = st.pop(); // 出栈res.add(cur.val);cur = cur.right;}}return res;}/*** 二叉树的非递归前序遍历** @param root* @return 前序遍历的节点列表*/public static List<Integer> preorderTraversal(TreeNode root) {// 定义resList<Integer> res = new ArrayList<>();if (root == null) {return res;}// 定义栈Stack<TreeNode> st = new Stack<>();st.push(root);while (!st.isEmpty()) {TreeNode tmp = st.pop(); // 出栈res.add(tmp.val);if (tmp.right != null) {st.push(tmp.right); // 先在栈中加入右节点,再加入左节点}if (tmp.left != null) {st.push(tmp.left); // 因为左节点要先出栈,栈是FILO的结构}}return res;}/*** 二叉树的非递归后序遍历** @param root* @return 后序遍历的节点列表*/public static List<Integer> postorderTraversal(TreeNode root) {List<Integer> res = new ArrayList<>();if (root == null) {return res;}Stack<TreeNode> st = new Stack<>();st.push(root);while (!st.isEmpty()) {TreeNode node = st.pop();res.add(node.val);// 注意:入栈顺序相对于前序遍历有改变// 前序遍历的入栈顺序是:中右左// 后序遍历的顺序变成了:中左右,然后反转result_listif (node.left != null) {st.push(node.left);}if (node.right != null) {st.add(node.right);}}Collections.reverse(res);return res;}public static void main(String[] args) {// 创建二叉树 [1,null,2,3]TreeNode root = new TreeNode(1);root.right = new TreeNode(2);root.right.left = new TreeNode(3);System.out.println(preorderTraversal(root));System.out.println(postorderTraversal(root));System.out.println(inorderTraversal(root));}
}
http://www.lryc.cn/news/336472.html

相关文章:

  • 【LeetCode刷题笔记】LeetCode 1365.有多少小于当前数字的数字
  • 室内定位中文综述阅读
  • 微信小程序uniapp+vue电力巡线任务故障报修管理系统2q91t
  • springboot国际化多语言
  • set和map
  • Open CASCADE学习|求曲面的参数空间
  • 代码随想录阅读笔记-二叉树【总结】
  • 【SpringBoot整合系列】SpringBoot整合FastDFS(二)
  • L2-2 巴音布鲁克永远的土(二分+并查集)
  • Spring Cloud学习笔记:Eureka简介,Eureka简单样例
  • 【漏洞复现】WordPress Welcart 任意文件读取漏洞(CVE-2022-4140)
  • 快速排序:深入解析其原理、实现与性能特性
  • 一文看懂Mac地址
  • 2024.4.10作业
  • python - Django创建项目
  • WPF —— 动画缩放变换
  • SQL注入---盲注
  • PlanUML和Mermaid哪个好?
  • leetcode 343. 整数拆分
  • 【MATLAB源码-第180期】基于matlab的PTS,SLM,CPFilter三种降低OFDM系统的PAPR仿真。
  • 学透Spring Boot — 004. Spring Boot Starter机制和自动配置机制
  • 面试算法-170-二叉树的最大深度
  • 【数据结构】哈希
  • Kubernetes(k8s)监控与报警(qq邮箱+钉钉):Prometheus + Grafana + Alertmanager(超详细)
  • STM32-04基于HAL库(CubeMX+MDK+Proteus)中断案例(按键中断扫描)
  • 第十五篇:Mybatis
  • 【MacBook系统homebrew镜像记录】
  • 深拷贝总结
  • RabbitMQ在云原生环境中部署和应用实践
  • flask 后端 + 微信小程序和网页两种前端:调用硬件(相机和录音)和上传至服务器