根据先序序列和中序序列构造二叉树进行层次遍历

2024-06-20 03:38

本文主要是介绍根据先序序列和中序序列构造二叉树进行层次遍历,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

基于遍历先序序列的每个元素分割中序序列为左右子树进行递归操作。

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Queue;public class Tree {static int[] pretemp;private static int maxLevel = 0;public class BinarayTreeNode {public int m_Value;public BinarayTreeNode m_LeftTreeNode = null;public BinarayTreeNode m_RigtTreeNode = null;}public BinarayTreeNode Construct(int[] preorder, int[] inorder) {if (preorder == null || inorder == null || inorder.length == 0 || preorder.length == 0) {return null;}int ptr = 0;int rootValue = preorder[0];BinarayTreeNode rootNode = new BinarayTreeNode();rootNode.m_Value = rootValue;while (ptr <= inorder.length && inorder[ptr] != rootValue) {ptr++;}pretemp = new int[preorder.length - 1];for (int i = 0; i < preorder.length; i++) {if (i == 0) {continue;}pretemp[i - 1] = preorder[i];}int[] inStart = new int[ptr];int[] inEnd = new int[inorder.length - ptr - 1];int ps = 0;for (int j = 0; j < inorder.length; j++) {if (j < ptr) {inStart[j] = inorder[j];}if (j > ptr) {inEnd[ps] = inorder[j];ps++;}}rootNode.m_LeftTreeNode = Construct(pretemp, inStart);rootNode.m_RigtTreeNode = Construct(pretemp, inEnd);return rootNode;}// print binarayTree as level modepublic static void PrintLevelTree(BinarayTreeNode pRoot) {Queue<BinarayTreeNode> nodeQueue = new LinkedList<BinarayTreeNode>();nodeQueue.add(pRoot);while (nodeQueue != null) {BinarayTreeNode node = nodeQueue.poll();if (node != null)System.out.print(node.m_Value);if (node != null && node.m_LeftTreeNode != null)nodeQueue.add(node.m_LeftTreeNode);if (node != null && node.m_RigtTreeNode != null)nodeQueue.add(node.m_RigtTreeNode);if (node == null) {System.out.println();break;}}}// get current binarayTree levelprivate static int getTreeLeverl(BinarayTreeNode pRoot, int level) {if (pRoot != null) {level++;}if( maxLevel < level ) maxLevel = level;if (pRoot.m_LeftTreeNode != null) {getTreeLeverl(pRoot.m_LeftTreeNode, level);} if (pRoot.m_RigtTreeNode != null) {getTreeLeverl(pRoot.m_RigtTreeNode, level);}return maxLevel ;}private static void printTreeLeverl(Map<Integer, String> printInfo, BinarayTreeNode pRoot, int level) {String value = printInfo.get(level);value = value == null ? "" : value;value = value + pRoot.m_Value +" " ;printInfo.put(level, value);level++;if (pRoot.m_LeftTreeNode != null) {printTreeLeverl(printInfo, pRoot.m_LeftTreeNode, level);}if (pRoot.m_RigtTreeNode != null) {printTreeLeverl(printInfo, pRoot.m_RigtTreeNode, level);}}public static void main(String[] args) {int preorder[] = { 1, 2, 4,9, 7, 0,8,3, 5, 6, 8 }; // 先序int inorder[] = { 9, 4, 7,8, 0,2, 1, 5, 3, 8, 6 }; // 中序//根据先序中序构造二叉树BinarayTreeNode root = new Tree().Construct(preorder, inorder);System.out.println("==========");int totollevel = getTreeLeverl(root, 0);System.out.println("二叉树的深度:"+totollevel);System.out.println("==========");System.out.print("二叉树的层次遍历:");PrintLevelTree(root);System.out.println("===========");System.out.println("二叉树的层次结构:");System.out.println("===========");Map<Integer, String> printInfo = new HashMap<Integer, String>();printTreeLeverl(printInfo, root, 1);for (Entry<Integer, String> map : printInfo.entrySet()) {System.out.println(map.getValue());}}
}

 

这篇关于根据先序序列和中序序列构造二叉树进行层次遍历的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1077000

相关文章

如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别详解

《如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别详解》:本文主要介绍如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别的相关资料,描述了如何使用海康威视设备网络SD... 目录前言开发流程问题和解决方案dll库加载不到的问题老旧版本sdk不兼容的问题关键实现流程总结前言作为

SpringBoot中使用 ThreadLocal 进行多线程上下文管理及注意事项小结

《SpringBoot中使用ThreadLocal进行多线程上下文管理及注意事项小结》本文详细介绍了ThreadLocal的原理、使用场景和示例代码,并在SpringBoot中使用ThreadLo... 目录前言技术积累1.什么是 ThreadLocal2. ThreadLocal 的原理2.1 线程隔离2

最长公共子序列问题的深度分析与Java实现方式

《最长公共子序列问题的深度分析与Java实现方式》本文详细介绍了最长公共子序列(LCS)问题,包括其概念、暴力解法、动态规划解法,并提供了Java代码实现,暴力解法虽然简单,但在大数据处理中效率较低,... 目录最长公共子序列问题概述问题理解与示例分析暴力解法思路与示例代码动态规划解法DP 表的构建与意义动

关于最长递增子序列问题概述

《关于最长递增子序列问题概述》本文详细介绍了最长递增子序列问题的定义及两种优化解法:贪心+二分查找和动态规划+状态压缩,贪心+二分查找时间复杂度为O(nlogn),通过维护一个有序的“尾巴”数组来高效... 一、最长递增子序列问题概述1. 问题定义给定一个整数序列,例如 nums = [10, 9, 2

Python利用PIL进行图片压缩

《Python利用PIL进行图片压缩》有时在发送一些文件如PPT、Word时,由于文件中的图片太大,导致文件也太大,无法发送,所以本文为大家介绍了Python中图片压缩的方法,需要的可以参考下... 有时在发送一些文件如PPT、Word时,由于文件中的图片太大,导致文件也太大,无法发送,所有可以对文件中的图

如何使用Spring boot的@Transactional进行事务管理

《如何使用Springboot的@Transactional进行事务管理》这篇文章介绍了SpringBoot中使用@Transactional注解进行声明式事务管理的详细信息,包括基本用法、核心配置... 目录一、前置条件二、基本用法1. 在方法上添加注解2. 在类上添加注解三、核心配置参数1. 传播行为(

Java实战之自助进行多张图片合成拼接

《Java实战之自助进行多张图片合成拼接》在当今数字化时代,图像处理技术在各个领域都发挥着至关重要的作用,本文为大家详细介绍了如何使用Java实现多张图片合成拼接,需要的可以了解下... 目录前言一、图片合成需求描述二、图片合成设计与实现1、编程语言2、基础数据准备3、图片合成流程4、图片合成实现三、总结前

在Mysql环境下对数据进行增删改查的操作方法

《在Mysql环境下对数据进行增删改查的操作方法》本文介绍了在MySQL环境下对数据进行增删改查的基本操作,包括插入数据、修改数据、删除数据、数据查询(基本查询、连接查询、聚合函数查询、子查询)等,并... 目录一、插入数据:二、修改数据:三、删除数据:1、delete from 表名;2、truncate

微服务架构之使用RabbitMQ进行异步处理方式

《微服务架构之使用RabbitMQ进行异步处理方式》本文介绍了RabbitMQ的基本概念、异步调用处理逻辑、RabbitMQ的基本使用方法以及在SpringBoot项目中使用RabbitMQ解决高并发... 目录一.什么是RabbitMQ?二.异步调用处理逻辑:三.RabbitMQ的基本使用1.安装2.架构

C++中使用vector存储并遍历数据的基本步骤

《C++中使用vector存储并遍历数据的基本步骤》C++标准模板库(STL)提供了多种容器类型,包括顺序容器、关联容器、无序关联容器和容器适配器,每种容器都有其特定的用途和特性,:本文主要介绍C... 目录(1)容器及简要描述‌php顺序容器‌‌关联容器‌‌无序关联容器‌(基于哈希表):‌容器适配器‌:(