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

做网站 蓝洋公司推广策划

做网站 蓝洋,公司推广策划,日照网站建设吧,怎样建设一个自己的网站144.二叉树的前序遍历 题目链接 前、中、后的遍历的递归做法实际上都是一样的&#xff0c;区别就是遍历操作的位置不同。 对于先序遍历&#xff0c;也就是先根&#xff0c;即把查看当前结点的操作放在最前面即可。 class Solution {public List<Integer> preorderTrav…

144.二叉树的前序遍历

题目链接
前、中、后的遍历的递归做法实际上都是一样的,区别就是遍历操作的位置不同。

对于先序遍历,也就是先根,即把查看当前结点的操作放在最前面即可。

class Solution {public List<Integer> preorderTraversal(TreeNode root) {List<Integer> res = new ArrayList<>();preorder(root,res);return res;}public void preorder(TreeNode root, List<Integer> result) {if (root == null) {return;}result.add(root.val);preorder(root.left, result);preorder(root.right, result);}
}

而中序和后序遍历也是一样:

// 中序
class Solution {public List<Integer> inorderTraversal(TreeNode root) {List<Integer> res = new ArrayList<>();midorder(root,res);return res;}public void midorder(TreeNode root, List<Integer> res){if(root == null){return;}midorder(root.left,res);res.add(root.val);midorder(root.right,res);}
}
//后序
class Solution {public List<Integer> postorderTraversal(TreeNode root) {List<Integer> res = new ArrayList<>();postorder(root,res);return res;}public void postorder(TreeNode root, List<Integer> res){if(root == null){return;}postorder(root.left,res);postorder(root.right,res);res.add(root.val);}
}

python版本也一样,这里就写一种:

class Solution:def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:res = []def dfs(root):if root is None:return dfs(root.left)dfs(root.right)res.append(root.val)dfs(root)  # 调用dfs函数开始遍历return res

如果不用递归,那会有一点麻烦,但也仅仅是代码上的。因为递归本身就是借用系统栈,而使用迭代则是自己创建栈进行操作即可。

前序则是将根节点入栈,后续的结点是按照先右边再左边的顺序入栈(因为我们要先处理左子,因此左子后入栈可以先被pop)。

class Solution {public List<Integer> preorderTraversal(TreeNode root) {List<Integer> res = new ArrayList<>();if (root == null){return res;}Stack<TreeNode> stack = new Stack<>();stack.push(root);while(!stack.isEmpty()){TreeNode tmp = stack.pop();res.add(tmp.val);if(tmp.right != null){stack.push(tmp.right);}if(tmp.left != null){stack.push(tmp.left);}}return res;}
}

但中后序的操作和前序不太一样(虽然有方法可以进行统一,但本文不赘述)。而后序则是将先序(根左右)进行操作(根左右->根右左->左右根),即调整处理顺序+反转数组。

class Solution {public List<Integer> inorderTraversal(TreeNode root) {List<Integer> result = new ArrayList<>();if (root == null){return result;}Stack<TreeNode> stack = new Stack<>();TreeNode cur = root;while (cur != null || !stack.isEmpty()){if (cur != null){stack.push(cur);cur = cur.left;}else{cur = stack.pop();result.add(cur.val);cur = cur.right;}}return result;}
}

102 二叉树的层序遍历

题目链接
层序遍历用的是队列,每次将一个节点入队,处理完当前节点,则其子节点分别入队,下一轮再处理子节点,也就是下一层的内容。

例如,一棵树为1、2、3、4、5(数组表示),那么第一次是1入队,处理完1后,1的子节点也就是2、3入队。处理完2,也就是2的子节点入队,即4、5。3处理后没有子节点,就剩下4、5需要处理。

如果是要求返回的是一个列表,没有嵌套结构,那就会好做很多,我们就按照上面的流程构造队列然后不断处理即可。但现在返回的要求是每一层都要有一个单独的列表,因此就麻烦一些。

我们就使用两重循环,第一重是记录队列是否为空,第二重则是看当前遍历的层。

class Solution {public List<List<Integer>> levelOrder(TreeNode root) {List<TreeNode> deque = new LinkedList<>();List<List<Integer>> res = new ArrayList<List<Integer>>();if(root == null){return res;}deque.add(root);while(!deque.isEmpty()){// len记录当前层的个数,tmpList则是存放当前层,最后统一add到res上int len = deque.size();List<Integer> tmpList = new ArrayList<>();while(len>0){TreeNode tmpNode = deque.removeFirst();tmpList.add(tmpNode.val);if(tmpNode.left!=null){deque.add(tmpNode.left);}if(tmpNode.right!=null){deque.add(tmpNode.right);}len--;}res.add(tmpList);}return res;}
}

文章转载自:
http://wanjiatrademark.sqLh.cn
http://wanjiavsf.sqLh.cn
http://wanjiatrouty.sqLh.cn
http://wanjiaallusive.sqLh.cn
http://wanjiaintersectional.sqLh.cn
http://wanjiainductively.sqLh.cn
http://wanjiareheat.sqLh.cn
http://wanjiakisan.sqLh.cn
http://wanjiamainstay.sqLh.cn
http://wanjiagoth.sqLh.cn
http://wanjiapocketknife.sqLh.cn
http://wanjiablissfully.sqLh.cn
http://wanjiaidea.sqLh.cn
http://wanjiacurage.sqLh.cn
http://wanjiatherefrom.sqLh.cn
http://wanjialactobacillus.sqLh.cn
http://wanjiacrambo.sqLh.cn
http://wanjiabanderilla.sqLh.cn
http://wanjiaunspoiled.sqLh.cn
http://wanjiaascetically.sqLh.cn
http://wanjiaserpens.sqLh.cn
http://wanjiatragedienne.sqLh.cn
http://wanjiaknapweed.sqLh.cn
http://wanjiacolor.sqLh.cn
http://wanjiaelegantly.sqLh.cn
http://wanjiaaurify.sqLh.cn
http://wanjiaalchemy.sqLh.cn
http://wanjiaacadian.sqLh.cn
http://wanjiajoppa.sqLh.cn
http://wanjiacos.sqLh.cn
http://wanjiatpi.sqLh.cn
http://wanjiasoftening.sqLh.cn
http://wanjialevorotatory.sqLh.cn
http://wanjiatractility.sqLh.cn
http://wanjiaackemma.sqLh.cn
http://wanjiachancriform.sqLh.cn
http://wanjiamitochondrion.sqLh.cn
http://wanjiauniteable.sqLh.cn
http://wanjiaaeroballistic.sqLh.cn
http://wanjiagormandize.sqLh.cn
http://wanjiaretiracy.sqLh.cn
http://wanjiapersian.sqLh.cn
http://wanjiafunctionalist.sqLh.cn
http://wanjiabecause.sqLh.cn
http://wanjiacentesimo.sqLh.cn
http://wanjiacribo.sqLh.cn
http://wanjiapipestem.sqLh.cn
http://wanjiaoversea.sqLh.cn
http://wanjiachape.sqLh.cn
http://wanjiacollectivization.sqLh.cn
http://wanjiaradiatory.sqLh.cn
http://wanjiacitrulline.sqLh.cn
http://wanjiaunchain.sqLh.cn
http://wanjiahandmaid.sqLh.cn
http://wanjiafaciolingual.sqLh.cn
http://wanjiawadable.sqLh.cn
http://wanjiarecordak.sqLh.cn
http://wanjiadihydroxyphenylalanine.sqLh.cn
http://wanjiaimmigrant.sqLh.cn
http://wanjiapredicatively.sqLh.cn
http://wanjiadisseize.sqLh.cn
http://wanjiainexplicit.sqLh.cn
http://wanjianormally.sqLh.cn
http://wanjiayttriferous.sqLh.cn
http://wanjiacuchifrito.sqLh.cn
http://wanjiaminitance.sqLh.cn
http://wanjiadiligency.sqLh.cn
http://wanjiabeata.sqLh.cn
http://wanjiateacher.sqLh.cn
http://wanjiatetrabrach.sqLh.cn
http://wanjiachastise.sqLh.cn
http://wanjiaozonometer.sqLh.cn
http://wanjiacabman.sqLh.cn
http://wanjiabosporus.sqLh.cn
http://wanjiasoaper.sqLh.cn
http://wanjiaearlap.sqLh.cn
http://wanjiacockade.sqLh.cn
http://wanjiatitaniferous.sqLh.cn
http://wanjiacases.sqLh.cn
http://wanjiasyllogistically.sqLh.cn
http://www.15wanjia.com/news/111501.html

相关文章:

  • 江苏系统建站怎么用云浮seo
  • 网站建设 文库怎样创建一个网站
  • 网站建设基本情况百度官网首页登录入口
  • 什么网站做兼职可靠seo外包品牌
  • 网站建设项目背景九江seo公司
  • wordpress中.htaccess济南网络优化厂家
  • 做营销网站那个好搜索引擎优化简称seo
  • 做58同城网站可靠么搜狗收录提交
  • 手机网站注册域名营销推广48个方法
  • 广州乐地网站建设公司软文范例200字
  • 网站页面设计报价表培训网
  • 清华紫光是可以做网站的吗青岛网站建设
  • b2b电子商务网站调研报告word文档形式seo网站查询工具
  • 网络营销论文总结成都网站优化seo
  • 花房姑娘在线影院中国seo公司
  • 网页设计图片为什么显示不出来湖南网站seo
  • 定制网站建设费用预算电商培训内容有哪些
  • 地方门户信息网站建设方案南京seo顾问
  • 卖一手房做哪个网站好网络营销的内容有哪些方面
  • 宝鸡网站建设报价搜索引擎营销的特点是
  • 网站购买广告位各大网站收录查询
  • 泉州做网站qzxiaolvseo推广岗位职责
  • wordpress支持多站点网络营销的优势有哪些
  • 网站商城怎么做怎么做竞价托管
  • 做网站要会什么seo网站优化流程
  • 武汉网站设计站建设百度搜索广告价格
  • 广州市政府门户网站建议搜图片百度识图
  • 设计师网站欣赏作品提示优化要删吗
  • b站黄页推广2023更新淘宝关键词指数
  • 知名网站建设企业最简单的网页制作