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

力扣(105. 从前序与中序遍历序列构造二叉树,106. 从中序与后序遍历序列构造二叉树)

题目1链接
题目1:
在这里插入图片描述

思路:使用前序确定根,使用中序分左右子树,分治法。

难点:如何控制递归确定左右子树。

/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode() : val(0), left(nullptr), right(nullptr) {}*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}* };*///遍历前序确定根,使用中序来分树class Solution {
public:TreeNode* findRoot(vector<int>& preorder, vector<int>& inorder, int& preindex, int left, int right){if(left>right){return nullptr;}//首先前序确定根TreeNode* root = new TreeNode(preorder[preindex]);//遍历中序,找根,分左右int rootindex = left;while(rootindex<=right){if(inorder[rootindex] == preorder[preindex])break;  //找到了!rootindex++;}preindex++;//   [left, rootindex-1] rootindex [rootindex+1, right]root->left = findRoot(preorder, inorder, preindex, left, rootindex-1);root->right = findRoot(preorder, inorder, preindex, rootindex+1, right);return root;}TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {int i = 0;return findRoot(preorder, inorder, i,0, inorder.size()-1);}
};

题目2链接

题目2:
在这里插入图片描述
题目1会了,题目二就一定会了!

注意:后序(左右根)从后往前确定根,中序分左右子树。
递归时,先遍历右子树,再遍历左子树。

/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode() : val(0), left(nullptr), right(nullptr) {}*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}* };*/
class Solution {
public:TreeNode* findRoot(vector<int>& inorder, vector<int>& postorder, int& postindex, int left, int right){if(left>right){return nullptr;}//首先后序确定根TreeNode* root = new TreeNode(postorder[postindex]);//遍历中序,找根,分左右int rootindex = left;while(rootindex<=right){if(inorder[rootindex] == postorder[postindex])break;  //找到了!rootindex++;}postindex--;//   [left, rootindex-1] rootindex [rootindex+1, right]root->right = findRoot(inorder, postorder, postindex, rootindex+1, right);root->left = findRoot(inorder, postorder, postindex, left, rootindex-1);return root;}TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {int i = postorder.size() - 1;return findRoot(inorder, postorder, i, 0, inorder.size()-1);}
};
http://www.lryc.cn/news/280124.html

相关文章:

  • 网络安全技术新手入门:在docker上安装dvwa靶场
  • Docker 介绍 及 支持的操作系统
  • 大模型实战营Day5 LMDeploy大模型量化部署实践
  • py连接sqlserver数据库报错问题处理。20009
  • LTESniffer:一款功能强大的LTE上下行链路安全监控工具
  • SQL语句详解二-DDL(数据定义语言)
  • web前端算法简介之链表
  • C++函数对象
  • 插件化简单介绍
  • [Beego]1.Beego简介以及beego环境搭建,bee脚手架的使用,创建,运行项目
  • Tomcat 静态资源访问与项目根路径设置(AI问答)
  • Docker实战09|使用AUFS包装busybox
  • 什么是uni.request()?如何使用它?
  • 用React给XXL-JOB开发一个新皮肤(一):环境搭建和项目初始化
  • 华为常用的命令——display,记得点赞收藏!
  • Costco攻入山姆大本营
  • 什么是常量?如何区分常量和变量?
  • uniapp返回上一页并刷新数据
  • LeetCode 0083.删除排序链表中的重复元素:模拟
  • Javaweb之SpringBootWeb案例新增部门的详细解析
  • 基于微信小程序的音乐平台 开源项目
  • uniapp 微信小程序跳转外部链接
  • 【STM32】FLASH闪存
  • 滴水内存地址堆栈
  • Laravel中的lockForUpdate悲观锁
  • BikeDNA(八)外在分析:OSM 与参考数据的比较2
  • 28 星际旋转
  • 测试人员必备基本功(3)
  • 记一次数据修复,需要生成十万条sql进行数据回滚
  • [paddle]paddlehub部署paddleocr的hubserving服务