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

重建二叉树-C++

分享一个大牛的人工智能教程。零基础通俗易懂风趣幽默希望你也加入到人工智能的队伍中来请轻击人工智能教程https://www.captainai.net/troubleshooter// 面试题7重建二叉树 // 题目输入某二叉树的前序遍历和中序遍历的结果请重建出该二叉树。假设输 // 入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1, // 2, 4, 7, 3, 5, 6, 8}和中序遍历序列{4, 7, 2, 1, 5, 3, 8, 6}则重建出 // 图2.6所示的二叉树并输出它的头结点。 #include exception #include cstdio #include stdexcept struct BinaryTreeNode { int m_nValue; BinaryTreeNode *m_pLeft; BinaryTreeNode *m_pRight; }; BinaryTreeNode *CreateBinaryTreeNode(int value) { BinaryTreeNode *pNode new BinaryTreeNode(); pNode-m_nValue value; pNode-m_pLeft nullptr; pNode-m_pRight nullptr; return pNode; } void ConnectTreeNodes(BinaryTreeNode *pParent, BinaryTreeNode *pLeft, BinaryTreeNode *pRight) { if (pParent ! nullptr) { pParent-m_pLeft pLeft; pParent-m_pRight pRight; } } void PrintTreeNode(const BinaryTreeNode *pNode) { if (pNode ! nullptr) { printf(value of this node is: %d\n, pNode-m_nValue); if (pNode-m_pLeft ! nullptr) printf(value of its left child is: %d.\n, pNode-m_pLeft-m_nValue); else printf(left child is nullptr.\n); if (pNode-m_pRight ! nullptr) printf(value of its right child is: %d.\n, pNode-m_pRight-m_nValue); else printf(right child is nullptr.\n); } else { printf(this node is nullptr.\n); } printf(\n); } void PrintTree(const BinaryTreeNode *pRoot) { PrintTreeNode(pRoot); if (pRoot ! nullptr) { if (pRoot-m_pLeft ! nullptr) PrintTree(pRoot-m_pLeft); if (pRoot-m_pRight ! nullptr) PrintTree(pRoot-m_pRight); } } void DestroyTree(BinaryTreeNode *pRoot) { if (pRoot ! nullptr) { BinaryTreeNode *pLeft pRoot-m_pLeft; BinaryTreeNode *pRight pRoot-m_pRight; delete pRoot; pRoot nullptr; DestroyTree(pLeft); DestroyTree(pRight); } } BinaryTreeNode *ConstructCore(int *startPreorder, int *endPreorder, int *startInorder, int *endInorder); BinaryTreeNode *Construct(int *preorder, int *inorder, int length) { if (preorder nullptr || inorder nullptr || length 0) return nullptr; return ConstructCore(preorder, preorder length - 1, inorder, inorder length - 1); } BinaryTreeNode *ConstructCore( int *startPreorder, int *endPreorder, int *startInorder, int *endInorder) { // 前序遍历序列的第一个数字是根结点的值 int rootValue startPreorder[0]; BinaryTreeNode *root new BinaryTreeNode(); root-m_nValue rootValue; root-m_pLeft root-m_pRight nullptr; if (startPreorder endPreorder) { if (startInorder endInorder *startPreorder *startInorder) return root; else throw std::logic_error(Invalid input.); } // 在中序遍历中找到根结点的值 int *rootInorder startInorder; while (rootInorder endInorder *rootInorder ! rootValue) rootInorder; if (rootInorder endInorder *rootInorder ! rootValue) throw std::logic_error(Invalid input.); int leftLength rootInorder - startInorder; int *leftPreorderEnd startPreorder leftLength; if (leftLength 0) { // 构建左子树 root-m_pLeft ConstructCore(startPreorder 1, leftPreorderEnd, startInorder, rootInorder - 1); } if (leftLength endPreorder - startPreorder) { // 构建右子树 root-m_pRight ConstructCore(leftPreorderEnd 1, endPreorder, rootInorder 1, endInorder); } return root; } // 测试代码 void Test(char *testName, int *preorder, int *inorder, int length) { if (testName ! nullptr) printf(%s begins:\n, testName); printf(The preorder sequence is: ); for (int i 0; i length; i) printf(%d , preorder[i]); printf(\n); printf(The inorder sequence is: ); for (int i 0; i length; i) printf(%d , inorder[i]); printf(\n); try { BinaryTreeNode *root Construct(preorder, inorder, length); PrintTree(root); DestroyTree(root); } catch (std::exception exception) { printf(Invalid Input.\n); } } // 普通二叉树 // 1 // / \ // 2 3 // / / \ // 4 5 6 // \ / // 7 8 void Test1() { const int length 8; int preorder[length] {1, 2, 4, 7, 3, 5, 6, 8}; int inorder[length] {4, 7, 2, 1, 5, 3, 8, 6}; Test(Test1, preorder, inorder, length); } // 所有结点都没有右子结点 // 1 // / // 2 // / // 3 // / // 4 // / // 5 void Test2() { const int length 5; int preorder[length] {1, 2, 3, 4, 5}; int inorder[length] {5, 4, 3, 2, 1}; Test(Test2, preorder, inorder, length); } // 所有结点都没有左子结点 // 1 // \ // 2 // \ // 3 // \ // 4 // \ // 5 void Test3() { const int length 5; int preorder[length] {1, 2, 3, 4, 5}; int inorder[length] {1, 2, 3, 4, 5}; Test(Test3, preorder, inorder, length); } // 树中只有一个结点 void Test4() { const int length 1; int preorder[length] {1}; int inorder[length] {1}; Test(Test4, preorder, inorder, length); } // 完全二叉树 // 1 // / \ // 2 3 // / \ / \ // 4 5 6 7 void Test5() { const int length 7; int preorder[length] {1, 2, 4, 5, 3, 6, 7}; int inorder[length] {4, 2, 5, 1, 6, 3, 7}; Test(Test5, preorder, inorder, length); } // 输入空指针 void Test6() { Test(Test6, nullptr, nullptr, 0); } // 输入的两个序列不匹配 void Test7() { const int length 7; int preorder[length] {1, 2, 4, 5, 3, 6, 7}; int inorder[length] {4, 2, 8, 1, 6, 3, 7}; Test(Test7: for unmatched input, preorder, inorder, length); } int main(int argc, char *argv[]) { Test1(); Test2(); Test3(); Test4(); Test5(); Test6(); Test7(); return 0; } // ------ Output ------ /* Test1 begins: The preorder sequence is: 1 2 4 7 3 5 6 8 The inorder sequence is: 4 7 2 1 5 3 8 6 value of this node is: 1 value of its left child is: 2. value of its right child is: 3. value of this node is: 2 value of its left child is: 4. right child is nullptr. value of this node is: 4 left child is nullptr. value of its right child is: 7. value of this node is: 7 left child is nullptr. right child is nullptr. value of this node is: 3 value of its left child is: 5. value of its right child is: 6. value of this node is: 5 left child is nullptr. right child is nullptr. value of this node is: 6 value of its left child is: 8. right child is nullptr. value of this node is: 8 left child is nullptr. right child is nullptr. Test2 begins: The preorder sequence is: 1 2 3 4 5 The inorder sequence is: 5 4 3 2 1 value of this node is: 1 value of its left child is: 2. right child is nullptr. value of this node is: 2 value of its left child is: 3. right child is nullptr. value of this node is: 3 value of its left child is: 4. right child is nullptr. value of this node is: 4 value of its left child is: 5. right child is nullptr. value of this node is: 5 left child is nullptr. right child is nullptr. Test3 begins: The preorder sequence is: 1 2 3 4 5 The inorder sequence is: 1 2 3 4 5 value of this node is: 1 left child is nullptr. value of its right child is: 2. value of this node is: 2 left child is nullptr. value of its right child is: 3. value of this node is: 3 left child is nullptr. value of its right child is: 4. value of this node is: 4 left child is nullptr. value of its right child is: 5. value of this node is: 5 left child is nullptr. right child is nullptr. Test4 begins: The preorder sequence is: 1 The inorder sequence is: 1 value of this node is: 1 left child is nullptr. right child is nullptr. Test5 begins: The preorder sequence is: 1 2 4 5 3 6 7 The inorder sequence is: 4 2 5 1 6 3 7 value of this node is: 1 value of its left child is: 2. value of its right child is: 3. value of this node is: 2 value of its left child is: 4. value of its right child is: 5. value of this node is: 4 left child is nullptr. right child is nullptr. value of this node is: 5 left child is nullptr. right child is nullptr. value of this node is: 3 value of its left child is: 6. value of its right child is: 7. value of this node is: 6 left child is nullptr. right child is nullptr. value of this node is: 7 left child is nullptr. right child is nullptr. Test6 begins: The preorder sequence is: The inorder sequence is: this node is nullptr. Test7: for unmatched input begins: The preorder sequence is: 1 2 4 5 3 6 7 The inorder sequence is: 4 2 8 1 6 3 7 Invalid Input. */

相关文章:

重建二叉树-C++

分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请轻击人工智能教程https://www.captainai.net/troubleshooter // 面试题7:重建二叉树 // 题目:输入某二叉树的前…...

煤矿智能化通信网络构建:从极端环境挑战到一体化方案实践

1. 项目概述:一次工业通信技术在传统能源领域的深度赋能实践最近刚结束的北京煤炭展,我们迈威通信的展台算是小火了一把。不少行业内的老朋友和新客户过来,聊得最多的不是我们的交换机、网关又出了什么新型号,而是“你们这套东西&…...

LSPatch:无需Root的Android应用模块化终极指南

LSPatch:无需Root的Android应用模块化终极指南 【免费下载链接】LSPatch LSPatch: A non-root Xposed framework extending from LSPosed 项目地址: https://gitcode.com/gh_mirrors/ls/LSPatch 你是否曾经羡慕iOS的越狱插件,却因Android设备未ro…...

AI智能体技能开发实战:从awesome-agent-skills到高效智能体构建

1. 项目概述:从技能清单到智能体构建的实战指南最近在折腾AI智能体(Agent)开发的朋友,估计都绕不开一个名字:awesome-agent-skills。这个由VoltAgent维护的开源项目,乍一看就是个GitHub上常见的“Awesome”…...

DeaDBeeF音频处理核心:DSP、重采样与均衡器技术详解

DeaDBeeF音频处理核心:DSP、重采样与均衡器技术详解 【免费下载链接】deadbeef DeaDBeeF Player 项目地址: https://gitcode.com/gh_mirrors/de/deadbeef DeaDBeeF Player是一款功能强大的开源音乐播放器,其卓越的音频处理能力离不开三大核心技术…...

Verilog数值转换:数字设计工程师必须掌握的底层规则与工程实践

1. 项目概述:为什么Verilog数值转换是数字设计的基石在数字电路设计和FPGA开发中,Verilog是我们描述硬件行为的主要语言。很多刚入行的朋友,包括我当年,都曾以为写Verilog就是写“另一种编程语言”,把C语言或Python的习…...

【NotebookLM+IEA/IRENA数据融合实战】:72小时内完成新型储能技术竞争力评估

更多请点击: https://codechina.net 第一章:NotebookLM能源技术研究 NotebookLM 是 Google 推出的基于 AI 的研究协作者工具,其核心能力在于对用户上传的文档进行语义理解与上下文驱动的问答。在能源技术研究领域,NotebookLM 可显…...

别再只用moviepy了!用Python的av库给视频批量加字幕,5分钟搞定

别再只用moviepy了!用Python的av库给视频批量加字幕,5分钟搞定 视频字幕添加是内容创作者的高频需求,无论是自媒体博主制作教程视频,还是教育工作者录制课程,精准的字幕不仅能提升观看体验,还能显著提高内容…...

AI工程师实战技能树:从特征工程到MLOps的完整指南

1. 项目概述与核心价值最近在GitHub上看到一个挺有意思的仓库,叫tqviet1978/ai-skills。光看名字,你可能会觉得这又是一个关于AI技能学习的普通教程合集。但当我点进去仔细研究后,发现它的定位和内容组织方式,与市面上大多数“AI学…...

图形引擎的跨平台之舞:Skia与Direct2D的深度对话

图形引擎的跨平台之舞:Skia与Direct2D的深度对话 【免费下载链接】skia Skia is a complete 2D graphic library for drawing Text, Geometries, and Images. See documentation for contribution instructions. 项目地址: https://gitcode.com/gh_mirrors/ski/sk…...

告别繁琐组态:用SVG + JavaScript 5分钟为你的工业设备创建可交互HMI组件

工业设备HMI组件开发革命:5分钟用SVGJavaScript打造智能交互界面 在工业自动化领域,人机界面(HMI)是连接设备与操作者的关键纽带。传统HMI开发往往陷入两个极端:要么使用笨重的组态软件进行繁琐配置,要么投入大量时间开发定制化界…...

如何用opendbc解决汽车CAN总线解码难题:一份完整的实践指南

如何用opendbc解决汽车CAN总线解码难题:一份完整的实践指南 【免费下载链接】opendbc a Python API for your car 项目地址: https://gitcode.com/gh_mirrors/op/opendbc 面对现代汽车复杂的电子控制系统,你是否曾经困惑于如何理解车辆内部的数据…...

浏览器串口调试革命:无需安装驱动,3分钟上手专业级串口助手

浏览器串口调试革命:无需安装驱动,3分钟上手专业级串口助手 【免费下载链接】SerialAssistant A serial port assistant that can be used directly in the browser. 项目地址: https://gitcode.com/gh_mirrors/se/SerialAssistant 还在为串口调试…...

Arm Neoverse V2内存架构与PCIe地址管理解析

1. Arm Neoverse V2内存架构设计精要 在Arm Neoverse V2的体系结构中,内存映射机制是其高性能计算能力的基石。这套架构通过精细的地址空间划分,实现了对各类硬件资源的高效管理。我们先来看一个典型的多芯片系统内存布局示例: Chip 0: 0x0…...

Cairo高级特性解析:泛型、Trait系统和元编程的深度应用

Cairo高级特性解析:泛型、Trait系统和元编程的深度应用 【免费下载链接】cairo Cairo is the first Turing-complete language for creating provable programs for general computation. 项目地址: https://gitcode.com/gh_mirrors/ca/cairo Cairo作为首个支…...

InstructPix2Pix:5分钟掌握AI图像编辑的终极指南

InstructPix2Pix:5分钟掌握AI图像编辑的终极指南 【免费下载链接】instruct-pix2pix 项目地址: https://gitcode.com/gh_mirrors/in/instruct-pix2pix 你是否曾经幻想过,只需一句话就能让图片中的对象变成你想要的样子?比如把普通的大…...

《从GIS前端到AIGC大厂:WebGIS、WebGL、Three.js技术栈的底层能力拆解与岗位适配指南》

前端GIS技术栈:从图形学底层到AIGC营销增长的全链路实战指南 (附大厂AI前端JD精准匹配与可落地项目) 🔖 目录理论篇:GIS中必学的图形学、WebGL、Three.js核心内容(含GIS实战细节) 1.1 计算机图形…...

终极指南:在Windows上安装安卓应用的简单解决方案

终极指南:在Windows上安装安卓应用的简单解决方案 【免费下载链接】APK-Installer An Android Application Installer for Windows 项目地址: https://gitcode.com/GitHub_Trending/ap/APK-Installer 你是否曾经希望在Windows电脑上直接运行手机应用&#xf…...

智能识别整理会议内容,让开会后怎么列待办更清晰更省事

作为经常跑客户、开会议的销售,此前我常被整理沟通内容、梳理待办的工作困扰,不仅耗时久,还容易漏记客户需求、搞错时间节点。结合大半年的实测体验,整理出一套AI整理方法,能快速清晰梳理待办,节省大量时间…...

如何免费解锁雀魂全角色皮肤:终极完整配置指南

如何免费解锁雀魂全角色皮肤:终极完整配置指南 【免费下载链接】majsoul_mod_plus 雀魂解锁全角色、皮肤、装扮等,支持全部服务器。 项目地址: https://gitcode.com/gh_mirrors/ma/majsoul_mod_plus 还在为无法获得心仪的雀魂角色而烦恼吗&#x…...

开发上下文管理工具:原理、实现与工程实践

1. 项目概述:一个为开发者量身定制的上下文管理工具如果你和我一样,每天要在多个项目、多种技术栈、甚至多个开发环境之间反复横跳,那你一定对“上下文切换”这个词深恶痛绝。我说的不是操作系统的上下文切换,而是我们开发者大脑里…...

Oto 多平台适配原理揭秘:从 Windows 到 Android 的底层实现

Oto 多平台适配原理揭秘:从 Windows 到 Android 的底层实现 【免费下载链接】oto ♪ A low-level library to play sound on multiple platforms ♪ 项目地址: https://gitcode.com/gh_mirrors/ot/oto Oto 是一个强大的跨平台音频播放库,支持从 W…...

如何快速搭建大众点评数据采集系统:Python爬虫完整指南

如何快速搭建大众点评数据采集系统:Python爬虫完整指南 【免费下载链接】dianping_spider 大众点评爬虫(全站可爬,解决动态字体加密,非OCR)。持续更新 项目地址: https://gitcode.com/gh_mirrors/di/dianping_spider…...

基于SpringBoot的民宿预订与评价系统毕业设计

博主介绍:✌ 专注于Java,python,✌关注✌私信我✌具体的问题,我会尽力帮助你。一、研究目的本研究旨在构建一个基于Spring Boot与Vue框架的民宿预订与评价系统以解决当前旅游住宿服务领域存在的信息不对称问题用户体验碎片化问题以及数据管理分散化问题该…...

Spring Boot Microservices故障排查:10个常见问题及解决方案

Spring Boot Microservices故障排查:10个常见问题及解决方案 【免费下载链接】spring-boot-microservices Spring Boot Template for Micro services Architecture - Show cases how to use Zuul for API Gateway, Spring OAuth 2.0 as Auth Server, Multiple Resou…...

基于SpringBoot的共享汽车管理系统毕设源码

博主介绍:✌ 专注于Java,python,✌关注✌私信我✌具体的问题,我会尽力帮助你。一、研究目的本研究旨在构建一个基于Spring Boot与Vue框架的共享汽车管理系统以解决当前共享汽车行业在资源调度效率、用户服务体验以及数据安全等方面存在的核心问题。随着城…...

从零打造专属机械键盘:基于CircuitPython的USB HID输入设备实践

1. 项目概述:打造你的专属“一键”键盘如果你对市面上千篇一律的键盘感到厌倦,或者一直想亲手制作一个独一无二的输入设备,那么这个项目就是为你准备的。今天,我们不谈那些复杂的全尺寸客制化键盘,而是从一个精巧、有趣…...

别再只会调占空比了!STM32F103驱动L298N电机,PWM模式1和模式2到底怎么选?

STM32F103驱动L298N电机:PWM模式1与模式2的深度实战解析 当你在调试L298N电机驱动模块时,是否遇到过这样的困惑:明明设置了相同的占空比,电机却表现出截然不同的响应特性?这背后往往隐藏着PWM模式选择的奥秘。对于STM3…...

第53节:倾斜模型osgb转3dtiles(免费工具)

1、下载cesiumlab工具 下载地址 2、启动cesiumlab,进行登录访问(网页版) 没有账号的可以用手机号注册一个 3、 选择倾斜模型切片 4、选择倾斜模型数据路径 5、设置空间参考、零点坐标 如果选择完osgb数据后能自动带出来则不用设置&…...

基于LangChain构建AI智能体:从核心架构到生产部署实战

1. 项目概述与核心价值最近在GitHub上看到一个名为“GenAI_Agents”的项目,作者是NirDiamant。这个项目名本身就很有意思,它直指当前AI领域最火热、也最具想象力的方向之一:智能体(Agents)。简单来说,这个项…...