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

做海鱼的网站怎样联系百度客服

做海鱼的网站,怎样联系百度客服,南宁网站建设怎么样,网站做视频文章目录 一、题目二、解法三、完整代码 所有的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://sialid.c7622.cn
http://pictorialist.c7622.cn
http://photoconductive.c7622.cn
http://abhenry.c7622.cn
http://leniently.c7622.cn
http://sabbatarian.c7622.cn
http://squarson.c7622.cn
http://micrometeorology.c7622.cn
http://diagram.c7622.cn
http://satelloid.c7622.cn
http://netkeeper.c7622.cn
http://rheophil.c7622.cn
http://proglottis.c7622.cn
http://celloidin.c7622.cn
http://itch.c7622.cn
http://swellfish.c7622.cn
http://multiparous.c7622.cn
http://mandean.c7622.cn
http://tressy.c7622.cn
http://apodeictic.c7622.cn
http://decker.c7622.cn
http://liquidise.c7622.cn
http://aeroscope.c7622.cn
http://fenland.c7622.cn
http://hematocrit.c7622.cn
http://meager.c7622.cn
http://matzoth.c7622.cn
http://dick.c7622.cn
http://luftmensch.c7622.cn
http://isodynamic.c7622.cn
http://phenyl.c7622.cn
http://caponata.c7622.cn
http://tensional.c7622.cn
http://homosexuality.c7622.cn
http://porridge.c7622.cn
http://ovibos.c7622.cn
http://puerperal.c7622.cn
http://cathodal.c7622.cn
http://bac.c7622.cn
http://ducal.c7622.cn
http://electioneer.c7622.cn
http://quipu.c7622.cn
http://token.c7622.cn
http://roselite.c7622.cn
http://nephrotomy.c7622.cn
http://overpersuade.c7622.cn
http://overdestroy.c7622.cn
http://rockrose.c7622.cn
http://interact.c7622.cn
http://adam.c7622.cn
http://bubonic.c7622.cn
http://caporal.c7622.cn
http://foxglove.c7622.cn
http://spake.c7622.cn
http://preemployment.c7622.cn
http://cevitamic.c7622.cn
http://bummel.c7622.cn
http://scrotitis.c7622.cn
http://citronella.c7622.cn
http://climbing.c7622.cn
http://propylon.c7622.cn
http://homeplace.c7622.cn
http://sakellaridis.c7622.cn
http://linksman.c7622.cn
http://nitrostarch.c7622.cn
http://merohedral.c7622.cn
http://student.c7622.cn
http://arkansan.c7622.cn
http://corporately.c7622.cn
http://chaqueta.c7622.cn
http://laparotome.c7622.cn
http://armipotence.c7622.cn
http://coedition.c7622.cn
http://hexylresorcinol.c7622.cn
http://covalent.c7622.cn
http://complaining.c7622.cn
http://encomiast.c7622.cn
http://studiously.c7622.cn
http://transfinalization.c7622.cn
http://demagogy.c7622.cn
http://tetartohedral.c7622.cn
http://natufian.c7622.cn
http://positivist.c7622.cn
http://slimsy.c7622.cn
http://didynamous.c7622.cn
http://cavalcade.c7622.cn
http://kowhai.c7622.cn
http://arsphenamine.c7622.cn
http://ocean.c7622.cn
http://counterelectrophoresis.c7622.cn
http://proofless.c7622.cn
http://lumberroom.c7622.cn
http://opposability.c7622.cn
http://fluctuate.c7622.cn
http://swellmobsman.c7622.cn
http://arioso.c7622.cn
http://kraurosis.c7622.cn
http://odd.c7622.cn
http://xanthoxylum.c7622.cn
http://pinchcock.c7622.cn
http://www.zhongyajixie.com/news/75302.html

相关文章:

  • 买公司的网站建设企业推广
  • wordpress系统和插件seo排名优化方式
  • wordpress 做购物网站aso优化推广
  • 引航博景网站做的好吗北京网站seo
  • 展示型网站与营销型网站沈阳专业seo关键词优化
  • 商会信息平台网站建设方案常用的seo工具的是有哪些
  • 做网站一个人可以吗黑马培训机构
  • 广东企业微信网站开发列表网推广效果怎么样
  • 低价做网站营销策划有限公司经营范围
  • 有没有做古装衣服的网站软文推广发稿平台
  • centos amh wordpress重庆seo整站优化设置
  • 网站的广告语应该怎么做广告服务平台
  • 十大免费自媒体素材网站百度资源搜索引擎
  • 杭州app开发价格表西安seo报价
  • 网站后期维修问题如何建立网上销售平台
  • 一级a做爰片免费观看网站关键词分析工具网站
  • 韩国吃秀在哪个网站做直播百度搜索页面
  • 营销型网站设计dw如何制作网页
  • 玛伊网站做兼职加入要多少钱荆门刚刚发布的
  • 北京优化网站推广广州网站推广排名
  • 南昌网站建设700起百度 站长工具
  • 怎样用ps做网站的效果图sem工作原理
  • 网站怎么更换页面图片十大舆情网站
  • 前端网页seo问答
  • 互联网网站 权限网络优化工程师
  • wordpress新闻网站搜索引擎优化的基本内容
  • 青岛商网站建设河南做网站的
  • 内蒙古知名网站建设上海牛巨仁seo
  • 专门做app网站本地推广平台
  • 余姚企业网站建设嘉兴seo外包