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

织梦网站会员上传图片商务网站开发课程体会

织梦网站会员上传图片,商务网站开发课程体会,网站代理网址,免费 网站 如何做摘要 博文主要介绍二叉树的前/中/后/层遍历(递归与栈)方法 一、前/中/后/层遍历问题 144. 二叉树的前序遍历 145. 二叉树的后序遍历 94. 二叉树的中序遍历 102. 二叉树的层序遍历 103. 二叉树的锯齿形层序遍历 二、二叉树遍历递归解析 // 前序遍历递归LC144_二叉树的前…摘要 博文主要介绍二叉树的前/中/后/层遍历(递归与栈)方法 一、前/中/后/层遍历问题 144. 二叉树的前序遍历 145. 二叉树的后序遍历 94. 二叉树的中序遍历 102. 二叉树的层序遍历 103. 二叉树的锯齿形层序遍历 二、二叉树遍历递归解析 // 前序遍历·递归·LC144_二叉树的前序遍历 class Solution {public ListInteger preorderTraversal(TreeNode root) {ListInteger result new ArrayListInteger();preorder(root, result);return result;}public void preorder(TreeNode root, ListInteger result) {if (root null) {return;}result.add(root.val);preorder(root.left, result);preorder(root.right, result);} }// 中序遍历·递归·LC94_二叉树的中序遍历 class Solution {public ListInteger inorderTraversal(TreeNode root) {ListInteger res new ArrayList();inorder(root, res);return res;}void inorder(TreeNode root, ListInteger list) {if (root null) {return;}inorder(root.left, list);list.add(root.val); // 注意这一句inorder(root.right, list);} }// 后序遍历·递归·LC145_二叉树的后序遍历 class Solution {public ListInteger postorderTraversal(TreeNode root) {ListInteger res new ArrayList();postorder(root, res);return res;}void postorder(TreeNode root, ListInteger list) {if (root null) {return;}postorder(root.left, list);postorder(root.right, list);list.add(root.val); // 注意这一句} }三、二叉树遍历栈解析 // 前序遍历顺序中-左-右入栈顺序中-右-左 class Solution {public ListInteger preorderTraversal(TreeNode root) {ListInteger result new ArrayList();if (root null){return result;}StackTreeNode stack new Stack();stack.push(root);while (!stack.isEmpty()){TreeNode node stack.pop();result.add(node.val);if (node.right ! null){stack.push(node.right);}if (node.left ! null){stack.push(node.left);}}return result;} }// 中序遍历顺序: 左-中-右 入栈顺序 左-右 class Solution {public ListInteger inorderTraversal(TreeNode root) {ListInteger result new ArrayList();if (root null){return result;}StackTreeNode 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;} }// 后序遍历顺序 左-右-中 入栈顺序中-左-右 出栈顺序中-右-左 最后翻转结果 class Solution {public ListInteger postorderTraversal(TreeNode root) {ListInteger result new ArrayList();if (root null){return result;}StackTreeNode stack new Stack();stack.push(root);while (!stack.isEmpty()){TreeNode node stack.pop();result.add(node.val);if (node.left ! null){stack.push(node.left);}if (node.right ! null){stack.push(node.right);}}Collections.reverse(result);return result;} }四、二叉树层序遍历解析 // 102.二叉树的层序遍历 class Solution {public ListListInteger resList new ArrayListListInteger();public ListListInteger levelOrder(TreeNode root) {//checkFun01(root,0);checkFun02(root);return resList;}public void checkFun02(TreeNode node) {if (node null) return;QueueTreeNode que new LinkedListTreeNode();que.offer(node);while (!que.isEmpty()) {ListInteger itemList new ArrayListInteger();int len que.size();while (len 0) {TreeNode tmpNode que.poll();itemList.add(tmpNode.val);if (tmpNode.left ! null) que.offer(tmpNode.left);if (tmpNode.right ! null) que.offer(tmpNode.right);len--;}resList.add(itemList);}} }博文参考 《leetcode》
http://www.zqtcl.cn/news/861026/

相关文章:

  • 做英文企业网站多钱钱wordpress调用外链图片
  • 自学网站查分数西双版纳傣族自治州天气
  • 网站建设一个多少钱wordpress朗读句子插件
  • 网站关键词怎么填写找代理商的渠道有哪些
  • 网站开发销售简历范文新建网站网络空间
  • 舟山外贸建站公司制作公司简介
  • 菜鸟是什么网站威海网站建设费用
  • 网站开发花费如何制作个人网页兼职
  • 网站鼠标特效用户体验最好的网站
  • 网站设计步骤图南通网站建设公司
  • 做盗版系统网站会不会开发次元世界
  • 下载爱南宁官方网站手机app开发软件有哪些
  • 云浮网站设计不收费的企业查询网站
  • 网站栏目怎么做iis网站筛选器被挂马
  • 网站开发中遇到的主要问题品牌营销策略包括哪些内容
  • 网站制作易捷网络十大社区团购平台有哪些
  • 哈尔滨口碑好的建站公司做网站制作一般多少钱
  • 河南网站网站制作华为品牌vi设计
  • 网站设置默认主页甘肃省第八建设集团公司网站
  • 自己做网站美工关键词优化排名网站
  • 淄博手机网站建设报价商业网站地方频道
  • 小说网站开发业务逻辑php 网站
  • 专业的做网站动态个人网站模板
  • 设计师网站设计网站开发试题库
  • 做网站是用c 吗东莞网络推广优化
  • 外贸soho网站建设wordpress配置搜索引擎优化
  • 嘉兴网站公司安卓优化大师2023
  • 电影网站开发影院座位问题正能量网站大全
  • dede手机网站更新成安专业做网站
  • 做能支付的网站贵吗品牌策划费用