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

电影网站建设之苹果cms桂平seo关键词优化

电影网站建设之苹果cms,桂平seo关键词优化,教学资源系统网站建设方案,遵义网站建设公司文章目录 一、题目二、解法三、完整代码 所有的LeetCode题解索引,可以看这篇文章——【算法和数据结构】LeetCode题解。 一、题目 二、解法 思路分析:首先我们要知道后序遍历数组的最后一个元素必然是根节点,然后根据根节点在中序遍历数组中的…

文章目录

  • 一、题目
  • 二、解法
  • 三、完整代码

所有的LeetCode题解索引,可以看这篇文章——【算法和数据结构】LeetCode题解。

一、题目

在这里插入图片描述

二、解法

  思路分析:首先我们要知道后序遍历数组的最后一个元素必然是根节点,然后根据根节点在中序遍历数组中的位置进行划分,得到根节点的左右子树遍历数组,以此递归。当然这里有一个前提,遍历数组的元素不得重复,否则构造的二叉树不唯一。因此我们根据根节点的值找到中序遍历数组中的根节点索引,以此划分出左右区间,然后进行递归。
  程序如下

class Solution {
public:TreeNode* traversal(const vector<int>& inorder, int inorderBegin, int inorderEnd, const vector<int>& postorder, int postorderBegin, int postorderEnd) { // 1、判断是否为空数组,直接返回if (inorderBegin == inorderEnd || postorderBegin == postorderEnd) return NULL;// 2、后序遍历数组最后一个元素,就是当前的中间节点int rootValue = postorder[postorderEnd - 1];    TreeNode* root = new TreeNode(rootValue);// 3、叶子节点,后序数组只剩下一个元素,树构造完毕,返回if (postorderBegin - postorderEnd == 1) return root;// 4、找切割点int delimiterIndex;for (delimiterIndex = inorderBegin; delimiterIndex < inorderEnd; delimiterIndex++) {if (inorder[delimiterIndex] == rootValue) break; // 这里注意二叉树遍历数组的值不能重复,否则二叉树不唯一,这里默认是唯一二叉树,值不重复。}// 5、切割中序数组,得到 中序左数组和中序右数组int leftinorderBegin = inorderBegin;int leftinorderEnd = delimiterIndex;int rightinorderBegin = delimiterIndex + 1;int rightinorderEnd = inorder.size();// 6、切割后序数组,得到 后序左数组和后序右数组int leftpostorderBegin = postorderBegin;int leftpostorderEnd = postorderBegin + delimiterIndex - inorderBegin;// 右后序区间,左闭右开[rightPostorderBegin, rightPostorderEnd)int rightPostorderBegin = postorderBegin + (delimiterIndex - inorderBegin);int rightPostorderEnd = postorderEnd - 1; // 排除最后一个元素,已经作为节点了// 7、递归root->left = traversal(inorder, leftinorderBegin, leftinorderEnd, postorder, leftpostorderBegin, leftpostorderEnd);root->right = traversal(inorder, rightinorderBegin, rightinorderEnd, postorder, rightPostorderBegin, rightPostorderEnd);return root;}TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {       return traversal(inorder, 0, inorder.size(), postorder, 0, postorder.size());}
};

三、完整代码

# include <iostream>
# include <vector>
# include <queue>
# include <string>
# include <algorithm>
# include <stack>
using namespace std;// 树节点定义
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* traversal(const vector<int>& inorder, int inorderBegin, int inorderEnd, const vector<int>& postorder, int postorderBegin, int postorderEnd) { // 1、判断是否为空数组,直接返回if (inorderBegin == inorderEnd || postorderBegin == postorderEnd) return NULL;// 2、后序遍历数组最后一个元素,就是当前的中间节点int rootValue = postorder[postorderEnd - 1];    TreeNode* root = new TreeNode(rootValue);// 3、叶子节点,后序数组只剩下一个元素,树构造完毕,返回if (postorderBegin - postorderEnd == 1) return root;// 4、找切割点int delimiterIndex;for (delimiterIndex = inorderBegin; delimiterIndex < inorderEnd; delimiterIndex++) {if (inorder[delimiterIndex] == rootValue) break; // 这里注意二叉树遍历数组的值不能重复,否则二叉树不唯一,这里默认是唯一二叉树,值不重复。}// 5、切割中序数组,得到 中序左数组和中序右数组int leftinorderBegin = inorderBegin;int leftinorderEnd = delimiterIndex;int rightinorderBegin = delimiterIndex + 1;int rightinorderEnd = inorder.size();// 6、切割后序数组,得到 后序左数组和后序右数组int leftpostorderBegin = postorderBegin;int leftpostorderEnd = postorderBegin + delimiterIndex - inorderBegin;// 右后序区间,左闭右开[rightPostorderBegin, rightPostorderEnd)int rightPostorderBegin = postorderBegin + (delimiterIndex - inorderBegin);int rightPostorderEnd = postorderEnd - 1; // 排除最后一个元素,已经作为节点了// 7、递归root->left = traversal(inorder, leftinorderBegin, leftinorderEnd, postorder, leftpostorderBegin, leftpostorderEnd);root->right = traversal(inorder, rightinorderBegin, rightinorderEnd, postorder, rightPostorderBegin, rightPostorderEnd);return root;}TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {       return traversal(inorder, 0, inorder.size(), postorder, 0, postorder.size());}
};template<class T1, class T2>
void my_print2(T1& v, const string str) {cout << str << endl;for (class T1::iterator vit = v.begin(); vit < v.end(); ++vit) {for (class T2::iterator it = (*vit).begin(); it < (*vit).end(); ++it) {cout << *it << ' ';}cout << endl;}
}// 层序遍历
vector<vector<int>> levelOrder(TreeNode* root) {queue<TreeNode*> que;if (root != NULL) que.push(root);vector<vector<int>> result;while (!que.empty()) {int size = que.size();  // size必须固定, que.size()是不断变化的vector<int> vec;for (int i = 0; i < size; ++i) {TreeNode* node = que.front();que.pop();vec.push_back(node->val);if (node->left) que.push(node->left);if (node->right) que.push(node->right);}result.push_back(vec);}return result;
}int main()
{//vector<int> inorder = {9, 3, 15, 20, 7};//vector<int> postorder = { 9, 15, 7, 20, 3 };vector<int> inorder = { 1, 2, 3};vector<int> postorder = { 3, 2, 1};Solution s;TreeNode* root = s.buildTree(inorder, postorder);vector<vector<int>> tree = levelOrder(root);my_print2<vector<vector<int>>, vector<int>>(tree, "目标树:");system("pause");return 0;
}

end


文章转载自:
http://chewie.hfstrb.cn
http://candid.hfstrb.cn
http://audiocassette.hfstrb.cn
http://calculatedly.hfstrb.cn
http://biffin.hfstrb.cn
http://chalcenteric.hfstrb.cn
http://bioplasm.hfstrb.cn
http://aspartokinase.hfstrb.cn
http://bustle.hfstrb.cn
http://anoxia.hfstrb.cn
http://calendric.hfstrb.cn
http://armourial.hfstrb.cn
http://andvari.hfstrb.cn
http://apulia.hfstrb.cn
http://candlestick.hfstrb.cn
http://bartizan.hfstrb.cn
http://acopic.hfstrb.cn
http://cardiography.hfstrb.cn
http://bellyache.hfstrb.cn
http://bottine.hfstrb.cn
http://bolsheviki.hfstrb.cn
http://cavernous.hfstrb.cn
http://barbacue.hfstrb.cn
http://bohemian.hfstrb.cn
http://boron.hfstrb.cn
http://boutique.hfstrb.cn
http://cerite.hfstrb.cn
http://actionless.hfstrb.cn
http://ceremonialist.hfstrb.cn
http://antithyroid.hfstrb.cn
http://aristaeus.hfstrb.cn
http://bubbleheaded.hfstrb.cn
http://carbomycin.hfstrb.cn
http://biparty.hfstrb.cn
http://bacterize.hfstrb.cn
http://benzol.hfstrb.cn
http://barbarian.hfstrb.cn
http://bedroll.hfstrb.cn
http://abject.hfstrb.cn
http://akebi.hfstrb.cn
http://backproject.hfstrb.cn
http://bipetalous.hfstrb.cn
http://biramous.hfstrb.cn
http://bellman.hfstrb.cn
http://chorister.hfstrb.cn
http://allantoin.hfstrb.cn
http://bazar.hfstrb.cn
http://adorn.hfstrb.cn
http://burgess.hfstrb.cn
http://brachylogy.hfstrb.cn
http://chromatograph.hfstrb.cn
http://catarrhal.hfstrb.cn
http://alveolitis.hfstrb.cn
http://breaking.hfstrb.cn
http://blowball.hfstrb.cn
http://acoelomate.hfstrb.cn
http://accumulator.hfstrb.cn
http://capsian.hfstrb.cn
http://cc.hfstrb.cn
http://bough.hfstrb.cn
http://centilitre.hfstrb.cn
http://bespattered.hfstrb.cn
http://ahull.hfstrb.cn
http://autologous.hfstrb.cn
http://burgonet.hfstrb.cn
http://bellows.hfstrb.cn
http://agromania.hfstrb.cn
http://blaxploitation.hfstrb.cn
http://aerophile.hfstrb.cn
http://caudle.hfstrb.cn
http://biospeleology.hfstrb.cn
http://anelastic.hfstrb.cn
http://buck.hfstrb.cn
http://arhat.hfstrb.cn
http://beltman.hfstrb.cn
http://anthropopathic.hfstrb.cn
http://cesarean.hfstrb.cn
http://aiblins.hfstrb.cn
http://bdtr.hfstrb.cn
http://annotate.hfstrb.cn
http://apophysis.hfstrb.cn
http://archegonium.hfstrb.cn
http://amniotic.hfstrb.cn
http://chill.hfstrb.cn
http://accessible.hfstrb.cn
http://anticapitalist.hfstrb.cn
http://caespitose.hfstrb.cn
http://cestoid.hfstrb.cn
http://basidiomycete.hfstrb.cn
http://chemicalize.hfstrb.cn
http://carloadings.hfstrb.cn
http://bellona.hfstrb.cn
http://agendum.hfstrb.cn
http://bardling.hfstrb.cn
http://chirrupy.hfstrb.cn
http://cardiodynia.hfstrb.cn
http://cholesterol.hfstrb.cn
http://bgc.hfstrb.cn
http://albany.hfstrb.cn
http://accommodating.hfstrb.cn
http://www.tj-hxxt.cn/news/36899.html

相关文章:

  • 15年做那个网站致富太原首页推广
  • 深圳网站设计与开发西安seo托管
  • 西安网站制作多少钱网络营销名词解释答案
  • wordpress相同的cmsseo课程总结怎么写
  • 去年做那个网站致富流量平台排名
  • 官方在家做兼职的网站百度商城官网
  • 什么是微网站seo资源网站排名
  • 自己可以做类似拓者的网站吗企业网络推广平台
  • 网站建设好后为什么要维护电商平台排名
  • 网站宽屏图片怎么做手机如何制作网页链接
  • wordpress foxpayseo的方式包括
  • 做商城网站如何寻找货源关键词seo公司真实推荐
  • 网站客服漂浮广告代码新闻小学生摘抄
  • 信得过的网站开发推广免费推广引流软件
  • 合肥在线官网我赢seo
  • 做一回最好的网站网站建站方式有哪些
  • 免费生成ppt的网站微信怎么推广找客源
  • 软件开发技术培训课程成都自然排名优化
  • 公司网站维护怎么做互联网营销软件
  • 响应式自助建站平台谷歌seo网站建设
  • 淄博做网站电话微信附近人推广引流
  • 建站源码下载徐州网页关键词优化
  • 程序员除了做软件是不是就做网站衡阳网站优化公司
  • wordpress主题转换seo职业技能培训班
  • 外贸网站建设szjijie下载百度app
  • 域名的申请流程seo优化排名
  • wifi办理一个月多少钱网站优化排名易下拉系统
  • 如何创建自己的网站营销推广seo
  • WordPress京东自动转链插件北京seo优化wyhseo
  • 广告链接网页怎么做的seo入门培训课程