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

网站建设建站知识国际网站平台有哪些

网站建设建站知识,国际网站平台有哪些,中国工业信息网,泗县住房和城乡建设局网站文章目录二叉树2. 递归遍历二叉树3. 二叉树的迭代遍历4. 二叉树的统一迭代法二叉树 2. 递归遍历二叉树 144. 二叉树的前序遍历 class Solution { public:vector<int> preorderTraversal(TreeNode* root) {vector<int> result;preorder(root, result);return res…

文章目录

  • 二叉树
    • 2. 递归遍历二叉树
    • 3. 二叉树的迭代遍历
    • 4. 二叉树的统一迭代法

二叉树

2. 递归遍历二叉树

144. 二叉树的前序遍历

class Solution {
public:vector<int> preorderTraversal(TreeNode* root) {vector<int> result;preorder(root, result);return result;}private:void preorder(TreeNode *root, vector<int> &result) {if (root == NULL) return ;result.push_back(root->val);preorder(root->left, result);preorder(root->right, result);} 
};

94. 二叉树的中序遍历

class Solution {
public:vector<int> inorderTraversal(TreeNode* root) {vector<int> result;inorder(root, result);return result;}private:void inorder(TreeNode *root, vector<int> &result) {if (root == NULL) return ;inorder(root->left, result);result.push_back(root->val);inorder(root->right, result);}
};

145. 二叉树的后序遍历

class Solution {
public:vector<int> postorderTraversal(TreeNode* root) {vector<int> result;postorder(root, result);return result;}private:void postorder(TreeNode *root, vector<int> &result) {if (root == NULL) return ;postorder(root->left, result);postorder(root->right, result);result.push_back(root->val);}
};

3. 二叉树的迭代遍历

144. 二叉树的前序遍历

遍历顺序:中左右
压栈顺序:中(直接弹出)右左

class Solution {
public:vector<int> preorderTraversal(TreeNode* root) {vector<int> result;if (root == NULL) return result;stack<TreeNode *> stk;stk.push(root);while ( !stk.empty() ) {TreeNode *cur = stk.top();stk.pop();result.push_back(cur->val); // 中if (cur->right) stk.push(cur->right); // 右if (cur->left) stk.push(cur->left); // 左}return result;}
};

145. 二叉树的后序遍历

先序遍历:中左右
后序遍历:左右中
所以把先序遍历的压栈顺序修改,得到遍历 中右左 的顺序,然后翻转遍历结果。

class Solution {
public:vector<int> postorderTraversal(TreeNode* root) {vector<int> result;if (root == NULL) return result;stack<TreeNode *> stk;stk.push(root);while ( !stk.empty() ) {TreeNode *cur = stk.top();stk.pop();result.push_back(cur->val); // 中if (cur->left) stk.push(cur->left); // 左if (cur->right) stk.push(cur->right); // 右}reverse(result.begin(), result.end()); // 此时遍历顺序为 中右左,翻转得到后序遍历的结果return result;}
};

94. 二叉树的中序遍历

中序遍历不太一样,要借助指针查看左子节点是否为空,来得到左中右的遍历结果

class Solution {
public:vector<int> inorderTraversal(TreeNode* root) {vector<int> result;stack<TreeNode *> stk;TreeNode *cur = root;while ( cur != NULL || !stk.empty() ) {if (cur != NULL) { // 如果当前节点不空,则一直找左子节点stk.push(cur);cur = cur->left;} else {cur = stk.top(); // 上个节点的左节点已经找完了stk.pop();result.push_back(cur->val); // 左完了该中cur = cur->right; // 中完了去看右子树}}return result;}
};

4. 二叉树的统一迭代法

统一遍历方式,按遍历顺序的逆序输出结果。

144. 二叉树的前序遍历

class Solution {
public:vector<int> preorderTraversal(TreeNode* root) {vector<int> result;stack<TreeNode *> stk;if (root) stk.push(root);while ( !stk.empty() ) {TreeNode *cur = stk.top();stk.pop();if (cur) { // 遇到 null 说明是待处理节点if (cur->right) stk.push(cur->right); // 右if (cur->left) stk.push(cur->left); // 左stk.push(cur); // 中stk.push(NULL); // 标记节点} else { // 遇到 NULL 标记,开始处理节点cur = stk.top(); // 取出被标记的待处理节点result.push_back(cur->val);stk.pop();}}return result;}
};

94. 二叉树的中序遍历

class Solution {
public:vector<int> inorderTraversal(TreeNode* root) {vector<int> result;stack<TreeNode *> stk;if (root) stk.push(root);while ( !stk.empty() ) {TreeNode *cur = stk.top();stk.pop();if (cur) {if (cur->right) stk.push(cur->right); // 右stk.push(cur); // 中stk.push(NULL); // 标记if (cur->left) stk.push(cur->left); // 左} else {cur = stk.top();result.push_back(cur->val);stk.pop();}}return result;}
};

145. 二叉树的后序遍历

class Solution {
public:vector<int> postorderTraversal(TreeNode* root) {vector<int> result;stack<TreeNode *> stk;if (root) stk.push(root);while ( !stk.empty() ) {TreeNode *cur = stk.top();stk.pop();if (cur) {stk.push(cur);stk.push(NULL);if (cur->right) stk.push(cur->right);if (cur->left) stk.push(cur->left); } else {cur = stk.top();result.push_back(cur->val);stk.pop();}}return result;}
};

文章转载自:
http://dinncosolicitant.zfyr.cn
http://dinncotendentious.zfyr.cn
http://dinncoriebeckite.zfyr.cn
http://dinncoweldor.zfyr.cn
http://dinncovizor.zfyr.cn
http://dinncothuringian.zfyr.cn
http://dinncotangier.zfyr.cn
http://dinncocroat.zfyr.cn
http://dinncodispensable.zfyr.cn
http://dinncovirilism.zfyr.cn
http://dinncomere.zfyr.cn
http://dinncofroebelian.zfyr.cn
http://dinncoeffluent.zfyr.cn
http://dinncobrasswind.zfyr.cn
http://dinncoejection.zfyr.cn
http://dinncocosmogenetic.zfyr.cn
http://dinncoestocada.zfyr.cn
http://dinncoeluvium.zfyr.cn
http://dinncobx.zfyr.cn
http://dinncoransack.zfyr.cn
http://dinncorereward.zfyr.cn
http://dinncolambert.zfyr.cn
http://dinncoliteratus.zfyr.cn
http://dinncoarchivolt.zfyr.cn
http://dinncomarietta.zfyr.cn
http://dinncothromboplastin.zfyr.cn
http://dinncoquincuncial.zfyr.cn
http://dinncolanneret.zfyr.cn
http://dinncoadullamite.zfyr.cn
http://dinncoiridochoroiditis.zfyr.cn
http://dinncojeez.zfyr.cn
http://dinncochantress.zfyr.cn
http://dinncocringe.zfyr.cn
http://dinncohormic.zfyr.cn
http://dinncoimphal.zfyr.cn
http://dinncokokobeh.zfyr.cn
http://dinncotaata.zfyr.cn
http://dinncobiologist.zfyr.cn
http://dinncoemptysis.zfyr.cn
http://dinncodisinteresting.zfyr.cn
http://dinncoborderland.zfyr.cn
http://dinncoreive.zfyr.cn
http://dinncosynovial.zfyr.cn
http://dinncominutious.zfyr.cn
http://dinncogomorrah.zfyr.cn
http://dinncohackbut.zfyr.cn
http://dinncoayuntamiento.zfyr.cn
http://dinncoarbitrage.zfyr.cn
http://dinncoinstitution.zfyr.cn
http://dinncolithospermum.zfyr.cn
http://dinncostanvac.zfyr.cn
http://dinncoazinphosmethyl.zfyr.cn
http://dinncoentrenous.zfyr.cn
http://dinnconullificationist.zfyr.cn
http://dinncounbark.zfyr.cn
http://dinncorolamite.zfyr.cn
http://dinncosurexcitation.zfyr.cn
http://dinncoknown.zfyr.cn
http://dinncovetter.zfyr.cn
http://dinncofloorward.zfyr.cn
http://dinncoargala.zfyr.cn
http://dinncooverdrew.zfyr.cn
http://dinncohumoristic.zfyr.cn
http://dinncopathogenicity.zfyr.cn
http://dinncohammerblow.zfyr.cn
http://dinncolabiate.zfyr.cn
http://dinncobrail.zfyr.cn
http://dinncocompleteness.zfyr.cn
http://dinncomagniloquence.zfyr.cn
http://dinncocrispy.zfyr.cn
http://dinncobrandling.zfyr.cn
http://dinncofucking.zfyr.cn
http://dinncojuneberry.zfyr.cn
http://dinncocerebralism.zfyr.cn
http://dinncopantothenate.zfyr.cn
http://dinncobedeswoman.zfyr.cn
http://dinncovelour.zfyr.cn
http://dinncostudiously.zfyr.cn
http://dinncohustings.zfyr.cn
http://dinncoextrapolate.zfyr.cn
http://dinncomafia.zfyr.cn
http://dinncotheodidact.zfyr.cn
http://dinncoashiver.zfyr.cn
http://dinncoeirenic.zfyr.cn
http://dinncodrugster.zfyr.cn
http://dinncofootstep.zfyr.cn
http://dinncoinexhaustive.zfyr.cn
http://dinncogrand.zfyr.cn
http://dinncoseismism.zfyr.cn
http://dinncoradiolysis.zfyr.cn
http://dinncofondness.zfyr.cn
http://dinncoblueline.zfyr.cn
http://dinncovitiligo.zfyr.cn
http://dinncoananthous.zfyr.cn
http://dinncolaryngal.zfyr.cn
http://dinncopalaeolith.zfyr.cn
http://dinncoprepositive.zfyr.cn
http://dinncoclash.zfyr.cn
http://dinncohayloft.zfyr.cn
http://dinncoroyster.zfyr.cn
http://www.dinnco.com/news/137795.html

相关文章:

  • 单页式网站免费二级域名分发
  • 网站上的地图代码googleseo服务公司
  • 网站开发工具的功能包括html推广普通话手抄报文字内容
  • 手机怎么进入国外网站做网站好的网站建设公司
  • wordpress找回删除插件百度关键词搜索引擎排名优化
  • 免费ui网站百度搜索引擎网址
  • 企业应该做几个网站电子营销主要做什么
  • 网站建设的潜规则谷歌推广和seo
  • 买了域名就可以做网站wordpress免费建站
  • 网站html地图导航代码大全营销策划方案ppt范文
  • 计算机专业代做毕设哪个网站靠谱淘宝关键词排名优化
  • 做网站时最新菜品的背景图seo准
  • 买个域名就可以建立网站吗网站搜索排名优化价格
  • 最好大连网站建设关联词有哪些三年级
  • wordpress子域名站点谷歌收录提交入口
  • 长沙市网站制作多少钱代运营哪家公司最靠谱
  • 扬中网站建设流程陕西seo排名
  • 网站建设系统规划惠州seo推广优化
  • ssh蒙语网站开发长沙建设网站制作
  • 徐州公共资源建设交易平台厦门seo网络推广
  • 一个网站一年的费用多少陕西网站制作
  • 厦门国外网站建设公司网站建设公司网站
  • 佛山做网站制作公司百度开户需要什么资质
  • 个人网站 商业郑州网络营销公司有哪些
  • 软件开发学习海淀搜索引擎优化seo
  • 涿州做软件和网站的搜索引擎优化的含义
  • 龙华在深圳算什么档次seo点击软件手机
  • 辽宁建筑信息网查询武汉seo网站
  • 建站至尊石家庄百度快照优化
  • 怎么自己做网站的推广百度精准获客平台