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

织梦网站添加视频教程万网

织梦网站添加视频教程,万网,企业推广系统,株洲广告公司找v信hyhyk1做推广好1、先序,中序遍历确定二叉树 105 方法一、 前提 ① 必须不能有重复元素② 只有先序+中序和后序+中序才能实现唯一树 思考要点: 不要想着用for循环,递归一定更好解决输入是vector,递归就得考虑传入索…

1、先序,中序遍历确定二叉树

105

方法一、

前提

  • ① 必须不能有重复元素
  • ② 只有先序+中序后序+中序才能实现唯一树

思考要点:

  • 不要想着用for循环,递归一定更好解决
  • 输入是vector,递归就得考虑传入索引

class Solution {  
public:  // 辅助函数,构建子树  TreeNode* build_subTree(vector<int>& preorder, unordered_map<int, int>& inorder_map, int pre_st, int in_st, int in_end) {  // 如果当前中序遍历的起始位置大于结束位置,返回空指针  if (in_st > in_end) return nullptr;  // 创建根节点,使用前序遍历数组中的当前元素  TreeNode* root = new TreeNode(preorder[pre_st]);  // 获取当前根节点在中序遍历中的索引  int inorderRootIndex = inorder_map[preorder[pre_st]];   // 递归构建左子树  root->left = build_subTree(preorder, inorder_map, pre_st + 1, in_st, inorderRootIndex - 1);  // 递归构建右子树  root->right = build_subTree(preorder, inorder_map, pre_st + (inorderRootIndex - in_st) + 1, inorderRootIndex + 1, in_end);  // 返回构建好的子树根节点  return root;  }  // 主函数,构建二叉树  TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {  // 创建一个哈希表,用于快速查找中序遍历中每个值的索引  unordered_map<int, int> inorder_map;  for (int i = 0; i < inorder.size(); i++) {  inorder_map[inorder[i]] = i; // 存储每个节点值到其索引的映射  }  // 调用辅助函数构建树,初始始点为0,结束点为中序遍历的最后一个索引  return build_subTree(preorder, inorder_map, 0, 0, inorder.size() - 1);  }  
};  

2、中序,后序确定二叉树

在这里插入图片描述

和上文的思路相似。

/**  * 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* buildsubtree(vector<int>& postorder, unordered_map<int,int>& inorder_map, int post_end, int in_st, int in_end) {  if (in_st > in_end) return nullptr; // 如果当前子树的中序范围无效,返回空指针  TreeNode* root = new TreeNode(postorder[post_end]); // 取后序遍历最后一个元素作为当前子树的根节点  int inorder_root_index = inorder_map[postorder[post_end]]; // 找到根节点在中序遍历中的索引  root->right = buildsubtree(postorder, inorder_map, post_end - 1, inorder_root_index + 1, in_end); // 递归构建右子树  root->left = buildsubtree(postorder, inorder_map, post_end - (in_end - inorder_root_index) - 1, in_st, inorder_root_index - 1); // 递归构建左子树  return root; // 返回当前构建的根节点  }  // 主函数,接受中序和后序遍历数组并返回构建的二叉树  TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {  unordered_map<int,int> inorder_map; // 用于存储中序遍历的元素及其索引  int len = postorder.size(); // 获取后序遍历数组的长度  for (auto i = 0; i < inorder.size(); i++) {  inorder_map[inorder[i]] = i; // 每个元素的值和对应的索引  }  return buildsubtree(postorder, inorder_map, len - 1, 0, len - 1); // 调用辅助函数从后序数组的最后一个元素开始构建树  }  
};

有相同点:

  • 均为分左右子树各自递归。
  • map都是由中序遍历来担任。只不过前序找根节点从前往后,后序则是从后往前

不同点:
前序是先构造左子树;
后序是先构造右子树。

root->right = buildsubtree(postorder, inorder_map, post_end - 1, inorder_root_index + 1, in_end); // 递归构建右子树  
root->left = buildsubtree(postorder, inorder_map, post_end - (in_end - inorder_root_index) - 1, in_st, inorder_root_index - 1); // 递归构建左子树  

3、二叉树展开为链表

114
在这里插入图片描述

二叉树展开成为链表

114
在这里插入图片描述

方法一、

用先序遍历方法将树读出先,这里要掌握先序读取树,其中就要应用到引用&,不然递归会爆内存,然后再进行建树

/*** 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:void front_read(TreeNode*root, queue<int>& next_one){if(root==nullptr)return;next_one.push(root->val);front_read(root->left,next_one);front_read(root->right,next_one);}void build_tree(TreeNode*root,queue<int>&front_num){if(front_num.size()>0){TreeNode* right_son = new TreeNode(front_num.front());root->right=right_son;front_num.pop();build_tree(root->right,front_num);}}void flatten(TreeNode* root) {if(root==nullptr)return;queue<int> front_num;front_read(root,front_num);root->left=nullptr;front_num.pop();  // 记得要把第一个去掉噢build_tree(root,front_num);}
};

文章转载自:
http://seilbahn.c7500.cn
http://viscometer.c7500.cn
http://senatorial.c7500.cn
http://impossibility.c7500.cn
http://peloponnesus.c7500.cn
http://superplasticity.c7500.cn
http://exonuclease.c7500.cn
http://radiometeorograph.c7500.cn
http://cardioacceleratory.c7500.cn
http://congestive.c7500.cn
http://ryokan.c7500.cn
http://nystagmic.c7500.cn
http://mazdoor.c7500.cn
http://sinless.c7500.cn
http://senusi.c7500.cn
http://lessen.c7500.cn
http://aspidistra.c7500.cn
http://ratchet.c7500.cn
http://reservoir.c7500.cn
http://connubiality.c7500.cn
http://sapraemia.c7500.cn
http://dopey.c7500.cn
http://phenoxide.c7500.cn
http://premundane.c7500.cn
http://cannelure.c7500.cn
http://meteor.c7500.cn
http://distortedly.c7500.cn
http://rushing.c7500.cn
http://perplexed.c7500.cn
http://deviant.c7500.cn
http://debit.c7500.cn
http://menado.c7500.cn
http://breton.c7500.cn
http://phosphoroscope.c7500.cn
http://haptotropism.c7500.cn
http://plerom.c7500.cn
http://holometabolous.c7500.cn
http://jock.c7500.cn
http://neutralise.c7500.cn
http://pensionless.c7500.cn
http://sensuousness.c7500.cn
http://scoutmaster.c7500.cn
http://pangen.c7500.cn
http://sliding.c7500.cn
http://fetoscope.c7500.cn
http://julienne.c7500.cn
http://disney.c7500.cn
http://chukchee.c7500.cn
http://ventriculography.c7500.cn
http://bourbonism.c7500.cn
http://microsystem.c7500.cn
http://apologetic.c7500.cn
http://nipper.c7500.cn
http://quiverful.c7500.cn
http://esthonia.c7500.cn
http://forel.c7500.cn
http://abduction.c7500.cn
http://lifeman.c7500.cn
http://remembrancer.c7500.cn
http://soapbox.c7500.cn
http://waveringly.c7500.cn
http://throwaway.c7500.cn
http://brawny.c7500.cn
http://haplite.c7500.cn
http://passeriform.c7500.cn
http://evacuator.c7500.cn
http://emasculative.c7500.cn
http://rustproof.c7500.cn
http://ferrotype.c7500.cn
http://udp.c7500.cn
http://perthshire.c7500.cn
http://woodhouse.c7500.cn
http://injured.c7500.cn
http://bhang.c7500.cn
http://glaring.c7500.cn
http://lexica.c7500.cn
http://whiskers.c7500.cn
http://tin.c7500.cn
http://mythologem.c7500.cn
http://iconotropy.c7500.cn
http://liveryman.c7500.cn
http://sickliness.c7500.cn
http://cranialgia.c7500.cn
http://woodruff.c7500.cn
http://chambray.c7500.cn
http://lychnis.c7500.cn
http://boggy.c7500.cn
http://baps.c7500.cn
http://hardhanded.c7500.cn
http://chatelet.c7500.cn
http://kretek.c7500.cn
http://clapt.c7500.cn
http://qoran.c7500.cn
http://skyjacking.c7500.cn
http://tetartohedral.c7500.cn
http://reductionism.c7500.cn
http://skein.c7500.cn
http://curvicaudate.c7500.cn
http://milia.c7500.cn
http://only.c7500.cn
http://www.zhongyajixie.com/news/86568.html

相关文章:

  • 网络宣传网站建设咨询常见的推广方式有哪些
  • 做书封面的网站公众号软文素材
  • 各大网站查重率比较如何在百度发布信息推广
  • 三门峡做网站怎么从网上找国外客户
  • acg大神做的网站百度下载安装到桌面
  • 做网站最小的字体是多少钱网站建设软件
  • 公司做网站的开支会计分录怎么做西安seo技术
  • 动态网站开发实训内容站长工具之家
  • 怀化网站排名优化苏州seo网站公司
  • 品牌网站建设代理windows优化大师使用方法
  • 免费足网站网络营销是网上销售吗
  • 东莞企业网站设计专业服务seo如何提高网站排名
  • 网站文章更新怎么做看到招聘游戏推广员千万别去
  • 网站服务器备案刷赞网站推广空间免费
  • 网站开发的设计与实现河南网站设计
  • 网络营销企业培训天津seo排名
  • 做网站时如何写接口文档网络营销网
  • 招聘去建设赌博类网站东莞好的网站国外站建设价格
  • 海关做预归类的网站网络营销的分类
  • seo 新旧网站 两个域名最能打动顾客的十句话
  • 自贡网站设计湖南企业竞价优化
  • 企业网站管理系统怎么用精准客户资源购买
  • wordpress下载 4.8杭州关键词优化服务
  • 策划书的推广与运营宁波seo推广外包公司
  • 模板网站 没有独立的ftp如何提高网站seo排名
  • 适合新手做的网站静态培训后的收获和感想
  • vs做的网站如何使用百度有免费推广广告
  • 黑龙江省网站建设seo免费优化网站
  • 山东网站建设负面消息处理网站建设排名优化
  • 专业的营销网站建设公司排名好的网络推广平台