mysql如何二叉树遍历_二叉树遍历
二叉树的遍历方式分别为:前序遍历、中序遍历、后序遍历。
前序遍历:
先访问根节点,再访问左节点,最后访问右节点
中序遍历:
先访问左节点,再访问根节点,最后访问右节点
后序遍历:
先访问左节点,再访问右节点,最后访问根节点
也就是根节点的访问顺序。
先定义一个二叉树节点
/**
* Definition for a binary tree node.
*/
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
public TreeNode(int val) {
this.val = val;
}
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
这里手动创建一个二叉树用于展示
/**
* 创建一个二叉树
*/
public static TreeNode createBinaryTree(){
TreeNode node1 = new TreeNode(1);
TreeNode node2 = new TreeNode(2);
TreeNode node3 = new TreeNode(3);
TreeNode node4 = new TreeNode(4);
TreeNode node5 = new TreeNode(5);
TreeNode node6 = new TreeNode(6);
TreeNode node7 = new TreeNode(7);
TreeNode node8 = new TreeNode(8);
TreeNode node9 = new TreeNode(9);
TreeNode node10 = new TreeNode(10);
node1.left = node2;
node1.right = node3;
node2.left = node4;
node2.right = node5;
node4.left = node8;
node5.left = node9;
node3.left = node6;
node3.right = node7;
node6.left = node10;
return node1;
}
构建完成的二叉树结构如下
binaryTree.png
递归方式
前序遍历
/**
* 前序遍历
* 根->左->右
* 输出 1 2 4 8 5 9 3 6 10 7
*/
public static void preorderTraversal(TreeNode root){
if (root == null){
return;
}
System.out.print(root.val + " ");
preorderTraversal(root.left);
preorderTraversal(root.right);
}
中序遍历
/**
* 中序遍历
* 左->根->右
* 输出 8 4 2 9 5 1 10 6 3 7
*/
public static void inorderTraversal(TreeNode root){
if (root == null){
return;
}
inorderTraversal(root.left);
System.out.print(root.val + " ");
inorderTraversal(root.right);
}
后序遍历
/**
* 后序遍历
* 左->右->根
* 输出 8 4 9 5 2 10 6 7 3 1
*/
public static void postorderTraversal(TreeNode root){
if (root == null){
return;
}
postorderTraversal(root.left);
postorderTraversal(root.right);
System.out.print(root.val + " ");
}
测试代码
public static void main(String[] args) {
TreeNode root = createBinaryTree();
// 1 2 4 8 5 9 3 6 10 7
preorderTraversal(root);
System.out.println();
// 8 4 2 9 5 1 10 6 3 7
inorderTraversal(root);
System.out.println();
// 8 4 9 5 2 10 6 7 3 1
postorderTraversal(root);
}
非递归方式
这里新建一个类用于记录节点状态
/**
* 记录节点状态
*/
public class Record {
// true 表示输出 false 表示继续访问
boolean flag;
TreeNode node;
public Record(TreeNode node, boolean flag){
this.flag = flag;
this.node = node;
}
}
前序遍历
/**
* 前序遍历
* 根->左->右
* 输出 1 2 4 8 5 9 3 6 10 7
*/
public static void preorderTraversal(TreeNode root){
if (root == null){
return;
}
Stack stack = new Stack<>();
stack.push(new Record(root,false));
while (!stack.isEmpty()){
Record Record = stack.pop();
if (Record.flag){
System.out.print(Record.node.val + " ");
}else {
if (Record.node.right != null){
stack.push(new Record(Record.node.right,false));
}
if (Record.node.left != null){
stack.push(new Record(Record.node.left,false));
}
stack.push(new Record(Record.node,true));
}
}
}
中序遍历
/**
* 中序遍历
* 左->根->右
* 输出 8 4 2 9 5 1 10 6 3 7
*/
public static void inorderTraversal(TreeNode root){
if (root == null){
return;
}
Stack stack = new Stack<>();
stack.push(new Record(root,false));
while (!stack.isEmpty()){
Record Record = stack.pop();
if (Record.flag){
System.out.print(Record.node.val + " ");
}else {
if (Record.node.right != null){
stack.push(new Record(Record.node.right,false));
}
stack.push(new Record(Record.node,true));
if (Record.node.left != null){
stack.push(new Record(Record.node.left,false));
}
}
}
}
后序遍历
/**
* 后序遍历
* 左->右->根
* 输出 8 4 9 5 2 10 6 7 3 1
*/
public static void postorderTraversal(TreeNode root){
if (root == null){
return;
}
Stack stack = new Stack<>();
stack.push(new Record(root,false));
while (!stack.isEmpty()){
Record Record = stack.pop();
if (Record.flag){
System.out.print(Record.node.val + " ");
}else {
stack.push(new Record(Record.node,true));
if (Record.node.right != null){
stack.push(new Record(Record.node.right,false));
}
if (Record.node.left != null){
stack.push(new Record(Record.node.left,false));
}
}
}
}
其实同递归方式一样只需要改变节点输出位置即可。
层序遍历
二叉树还有一种遍历方式层序遍历,即逐层地从左到右访问所有节点。与前三种方式不同这里需要借助队列先进先出的特点。
具体思路是在队列中每取到一个节点会把该节点的左右子节点分别添加到队列中
/**
* 层序遍历
* 输出 1 2 3 4 5 6 7 8 9 10
*/
public static void levelOrderTraveral(TreeNode root) {
Queue queue = new LinkedList<>();
// offer 把根节点添加到队列中
queue.offer(root);
while (!queue.isEmpty()) {
// 移除队列头部并打印
TreeNode treeNode = queue.poll();
System.out.print(treeNode.val + " ");
// 把左节点添加到队列中
if (treeNode.left != null) {
queue.offer(treeNode.left);
}
// 把右节点添加到队列中
if (treeNode.right != null) {
queue.offer(treeNode.right);
}
}
}
mysql如何二叉树遍历_二叉树遍历相关推荐
- 前序遍历二叉树代码_二叉树遍历、二叉树深度、代码示例,一点课堂(多岸学院)...
二叉树的遍历 ★★★★★TreeNode 节点/ Definition for a binary tree node. /public class TreeNode {int val;TreeNode ...
- java二叉树的深度优先遍历_二叉树的广度优先遍历、深度优先遍历的递归和非递归实现方式...
1 packageSolution;2 3 importjava.util.LinkedList;4 importjava.util.Queue;5 importjava.util.Stack;6 7 ...
- 广度优先遍历类似于二叉树的_二叉树的各种遍历方法的简单解释
二叉树顾名思义,最多两个孩子. 一般规定一个二叉树,因为节点间有相互连接的原因,所以只要给定根节点,那么顺着寻找左孩子和右孩子便可以遍历到所有的节点,这就是遍历的直观解释. 而遍历分为深度遍历和广度遍 ...
- 多叉树的前序遍历_二叉树的非递归遍历的思考
封面图来自wikipedia 1 简介 二叉树的深度优先遍历(前序遍历.中序遍历.后序遍历)是一个比较基本的操作.如果使用递归的做法,很容易写出相应的程序:而如果使用非递归的做法,虽然也能写出相应的代 ...
- 二叉树后序遍历_二叉树后序遍历非递归实现
二叉树的后序遍历非递归实现是三种遍历实现里面最复杂的一种了. 后序遍历的顺序是左节点-右节点-根节点,因为二叉树每个节点只有指向子节点的指针而没有指向父节点的指针,因此我们需要一个额外的变量来记录是否 ...
- resultset不支持循环遍历_二叉树的各种遍历方法的简单解释
二叉树顾名思义,最多两个孩子. 一般规定一个二叉树,因为节点间有相互连接的原因,所以只要给定根节点,那么顺着寻找左孩子和右孩子便可以遍历到所有的节点,这就是遍历的直观解释. 而遍历分为深度遍历和广度遍 ...
- java实现二叉树广度优先遍历_二叉树之深度优先和广度优先遍历(Java)
tree.png 1. 二叉树结构定义 public static class Tree { int data; Tree left; Tree right; public Tree(int data ...
- 广度优先遍历类似于二叉树的_深度优先遍历类似于二叉树的()
(1)[◆题库问题◆]:[单选] 深度优先遍历类似于二叉树的() A.先序遍历 B.中序遍历 C.后序遍历 D.层次遍历 [◆参考答案◆]:A ·ℳ°.·※°∴ ╰☆╮ .·ℳ°.·※°∴ ╰☆╮ . ...
- c++ 结构体遍历_二叉树(Binary Tree)的建立与遍历——C语言实现
一.运行环境简介 编辑器:VSCode + MicroSoft原生插件; :cat::dragon:运行环境: MinGW ; :cat::bust_in_silhouette:常用指令: gcc ...
最新文章
- 【BZOJ】3527: [Zjoi2014]力(fft+卷积)
- 文本分类入门(九)文本分类问题的分类
- 【算法知识】详解选择排序算法
- java什么是重构 何时使用重构_Java 之重构现有系统实战(一)
- 20. C# -- Base, this 关键字
- java js获取css方法_5种JavaScript和CSS交互的方法
- struts2中文件上传
- paip.获取proxool的配置 xml读取通过jdk xml 初始化c3c0在代码中总结
- 登录页面html5 css3 js代码,H5+css3+js搭建带验证码的登录页面
- web 开发之js---理解并解决IE的内存泄漏方式
- ubuntu16.04安装cuda11.3、cudnn8.2.1、tensorrt8.0.3.4全过程
- 漫画:不止于存储的智能云相册
- CISSP考试要涨价了,5月1日起考试费涨为749美元
- TechParty Mini+4 logging
- Linux磁盘空间说明
- python 柱状图如何添加数字标签_matplotlib可视化之如何给图形添加数据标签?
- C++中cout的基础语法与换行符endl的用法
- 金额保留小数点后两位方法
- 林业调查规划设计资质怎么办理?
- Leecode- 584. 寻找用户推荐人
热门文章
- window脚本自动发邮件
- ios jenkins配置_ios jenkins从0快速配置
- 又来了!自动完成淘宝双十一喵币浏览任务(Python)(可能防封)
- 计算机涉密网络,[网络部] 关于加强计算机涉密信息管理的规定
- 物联网和智慧地球的智慧
- 【KV260】解决xilinx-k26-starterkit-v2021.1-final.bsp在petalinux2021.1下配置及建立工程导致的问题
- 管道/查明文件夹中图片个数
- 誉达计算机外语学校,英语简历表格
- 总结一下工作中遇到的NPOI已经在ASP.NET MVC中的使用
- go 调用c 编译器找不到方法_深度解密Go语言之关于 interface 的10个问题