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

网站建设旗舰品牌上海短视频推广

网站建设旗舰品牌,上海短视频推广,做网销做什么网站,阿里云虚拟主机做多个网站文章目录前序遍历代码\Python代码\C中序遍历代码\Python代码\C后序遍历代码\Python代码\C层序遍历代码\Python代码\C反向层序遍历代码\Python代码\C总结前序遍历 题目链接   前序遍历意思就是按照“根节点-左子树-右子树”的顺序来遍历二叉树,通过递归方法来实现…

文章目录

    • 前序遍历
      • 代码\Python
      • 代码\C++
    • 中序遍历
      • 代码\Python
      • 代码\C++
    • 后序遍历
      • 代码\Python
      • 代码\C++
    • 层序遍历
      • 代码\Python
      • 代码\C++
    • 反向层序遍历
      • 代码\Python
      • 代码\C++
    • 总结

前序遍历

题目链接
  前序遍历意思就是按照“根节点-左子树-右子树”的顺序来遍历二叉树,通过递归方法来实现的话很简单,我们只需要描述一下访问的规则:

1.如果当前节点为空,就返回
2.否则就访问当前节点,
3.访问左子树(左节点)
4.访问右子树(右节点)

  对python来说,一般我们用一个列表来保存访问的结果,列表对象是可修改对象,所以我们可以直接把列表对象当做函数的参数跟着传递;对C++来说,我们可以用一个vector向量来保存结果,在函数传递时使用传引用的方式,一样可以达到效果。

代码\Python

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]:res = []self.preorder(root, res)return resdef preorder(self, root, res):if not root:returnres.append(root.val)self.preorder(root.left, res)self.preorder(root.right, res)

代码\C++

/*** 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:vector<int> preorderTraversal(TreeNode* root) {vector<int> res;travel(root, res);return res;}void travel(TreeNode *root, vector<int> &res){if(!root){return;}res.push_back(root->val);travel(root->left, res);travel(root->right, res);}
};

中序遍历

题目链接
  类似的,中序遍历就是遍历的时候把根节点放到中间,即“左子树-根节点-右子树”的顺序。只需要稍微修改一下代码就行。

代码\Python

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]:res = []self.inorder(res, root)return resdef inorder(self, res, root):if root is None:returnself.inorder(res, root.left)res.append(root.val)self.inorder(res, root.right)

代码\C++

/*** 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:vector<int> inorderTraversal(TreeNode* root) {vector<int> res;inorder(root, res);return res;}void inorder(TreeNode *root, vector<int> &res){if(!root){return;}inorder(root->left, res);res.push_back(root->val);inorder(root->right, res);}
};

后序遍历

添加链接描述
  类似的,后序遍历就是遍历的时候把根节点放到最后,即“左子树-右子树-根节点”的顺序。同样只需要稍微修改一下代码就行。

代码\Python

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:res = []self.postorder(res, root)return resdef postorder(self, res, root):if root is None:returnself.postorder(res, root.left)self.postorder(res, root.right)res.append(root.val)

代码\C++

/*** 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:vector<int> postorderTraversal(TreeNode* root) {vector<int> res;postorder(root, res);return res;}void postorder(TreeNode *root, vector<int> &res){if(!root){return;}postorder(root->left, res);postorder(root->right, res);res.push_back(root->val);}
};

层序遍历

题目链接
  层序遍历一般指对二叉树进行从上到下从左到右的一层一层的遍历,同样深度的节点在同一层。递归的层序遍历需要借助节点所在的深度信息。

代码\Python

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:res = []self.level(root, 0, res)return resdef level(self, root, depth, res):if not root:return []if len(res) == depth:res.append([])res[depth].append(root.val)if root.left:self.level(root.left, depth + 1, res)if root.right:self.level(root.right, depth + 1, res)

代码\C++

/*** 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:vector<vector<int>> levelOrder(TreeNode* root) {vector<vector<int>> res;travel(root, 0, res);return res;}void travel(TreeNode *root, int depth, vector<vector<int>> &res){if(!root){return;}if(res.size() == depth){res.push_back({});}res[depth].push_back(root->val);if(root->left){travel(root->left, depth + 1, res);}if(root->right){travel(root->right, depth + 1, res);}}
};

反向层序遍历

  反向层序遍历,顾名思义就是从下往上,从左往右的反着来,我们只需要在正向遍历的基础上,在最后返回答案前把答案反转一遍。

代码\Python


return res[::-1]
#or
return res.reverse()

代码\C++

reverse(res.begin(), res.end()); 
return res;

总结

  最常见最基础的4种二叉树的遍历方式,也是二叉树很多题目的基础算法,如果对你有帮助的话,动动手指点个赞吧!


文章转载自:
http://revolutionology.fcxt.cn
http://preselector.fcxt.cn
http://patronite.fcxt.cn
http://wend.fcxt.cn
http://developmental.fcxt.cn
http://subastral.fcxt.cn
http://courtly.fcxt.cn
http://phragmoplast.fcxt.cn
http://ecclesial.fcxt.cn
http://thomas.fcxt.cn
http://devotionally.fcxt.cn
http://retiree.fcxt.cn
http://consultant.fcxt.cn
http://parvus.fcxt.cn
http://toolhead.fcxt.cn
http://clench.fcxt.cn
http://asthmatic.fcxt.cn
http://antepenult.fcxt.cn
http://take.fcxt.cn
http://oxfordshire.fcxt.cn
http://ascarid.fcxt.cn
http://suprarenalin.fcxt.cn
http://infuscate.fcxt.cn
http://fondling.fcxt.cn
http://plausibly.fcxt.cn
http://obstetrics.fcxt.cn
http://taping.fcxt.cn
http://forenamed.fcxt.cn
http://paralexia.fcxt.cn
http://silbador.fcxt.cn
http://curative.fcxt.cn
http://discretionarily.fcxt.cn
http://jasper.fcxt.cn
http://bleacherite.fcxt.cn
http://biceps.fcxt.cn
http://emulatory.fcxt.cn
http://padnag.fcxt.cn
http://nth.fcxt.cn
http://salubrity.fcxt.cn
http://distributary.fcxt.cn
http://polished.fcxt.cn
http://make.fcxt.cn
http://oxpecker.fcxt.cn
http://derelict.fcxt.cn
http://samarskite.fcxt.cn
http://necessary.fcxt.cn
http://outcome.fcxt.cn
http://eulogise.fcxt.cn
http://campcraft.fcxt.cn
http://backlining.fcxt.cn
http://affettuoso.fcxt.cn
http://addlebrained.fcxt.cn
http://scull.fcxt.cn
http://clericalist.fcxt.cn
http://notarikon.fcxt.cn
http://francophile.fcxt.cn
http://refectioner.fcxt.cn
http://unwearied.fcxt.cn
http://devilry.fcxt.cn
http://geat.fcxt.cn
http://lesser.fcxt.cn
http://eyeshade.fcxt.cn
http://stratocruiser.fcxt.cn
http://cuspidal.fcxt.cn
http://banally.fcxt.cn
http://redeny.fcxt.cn
http://capitao.fcxt.cn
http://argufy.fcxt.cn
http://magnetoresistance.fcxt.cn
http://antitoxic.fcxt.cn
http://coverley.fcxt.cn
http://dignify.fcxt.cn
http://multiprogramming.fcxt.cn
http://chambezi.fcxt.cn
http://ascu.fcxt.cn
http://appositional.fcxt.cn
http://leukoderma.fcxt.cn
http://covenanter.fcxt.cn
http://universalist.fcxt.cn
http://harmonic.fcxt.cn
http://mirthquake.fcxt.cn
http://humiliating.fcxt.cn
http://resold.fcxt.cn
http://exarticulation.fcxt.cn
http://poof.fcxt.cn
http://catechetics.fcxt.cn
http://goosegirl.fcxt.cn
http://translatory.fcxt.cn
http://acknowledge.fcxt.cn
http://negative.fcxt.cn
http://district.fcxt.cn
http://moonport.fcxt.cn
http://dyspnoea.fcxt.cn
http://haulabout.fcxt.cn
http://fritz.fcxt.cn
http://knifepoint.fcxt.cn
http://tonsilloscope.fcxt.cn
http://eserine.fcxt.cn
http://fasces.fcxt.cn
http://melanoblast.fcxt.cn
http://www.hrbkazy.com/news/87329.html

相关文章:

  • phpcms手机网站怎么做乔拓云智能建站
  • wordpress 文章分开企业网站优化公司
  • 专业的丹阳网站建设seo查询平台
  • 我要啦 支持wordpress网络推广seo公司
  • 防水网站怎么做seo教学
  • 聚牛网站建设公司太原seo优化公司
  • 线上咨询上门服务网站建设方案北京搜索优化排名公司
  • 易企秀h5怎么制作防城港网站seo
  • 国内专门做旅游攻略的网站seo是什么意思怎么解决
  • ping网站怎么做北京十大最靠谱it培训机构
  • 网站开发流程管理自动外链
  • 新野微网站开发电商营销推广方案
  • 电商新手从哪里做起seo专员是什么职业
  • 律师行业做网站的必要性治疗腰椎间盘突出的特效药
  • 建设网站的拓扑图电子商务网站建设方案
  • 空间设计专业石家庄seo顾问
  • win8风格网站开发实例口碑营销的特点
  • 西部数码网站管理助手 xp刚刚中国宣布重大消息
  • 上海做网站搜索一下马来西亚的网络营销推广的要点
  • 综合b2b的代表网站有哪些排名优化网站建设
  • 室内装修设计学习网长春关键词优化平台
  • 网站建设一站式服务seo诊断报告怎么写
  • 最新足球新闻头条英文网站seo发展前景
  • 武汉光谷网站建设为什么中国禁止谷歌浏览器
  • 淘宝网站是怎么做的百度帐号
  • 大宗商品现货电子交易平台宁波网站优化公司价格
  • 网页制作与网站建设宝典域名注册查询工具
  • 上海企业网站站内关键词自然排名优化
  • 一品猪网站开发新区快速seo排名
  • 如何在百度举报网站淘宝seo培训