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

网站的管理跟新维护有哪些网站推广优化建设

网站的管理跟新维护有哪些,网站推广优化建设,建网站赚钱方法,wordpress 短代码 插件摘要 博文主要介绍二叉树的前/中/后/层遍历(递归与栈)方法 一、前/中/后/层遍历问题 144. 二叉树的前序遍历 145. 二叉树的后序遍历 94. 二叉树的中序遍历 102. 二叉树的层序遍历 103. 二叉树的锯齿形层序遍历 二、二叉树遍历递归解析 // 前序遍历递归LC144_二叉树的前…

摘要

博文主要介绍二叉树的前/中/后/层遍历(递归与栈)方法

一、前/中/后/层遍历问题

144. 二叉树的前序遍历

145. 二叉树的后序遍历

94. 二叉树的中序遍历

102. 二叉树的层序遍历

103. 二叉树的锯齿形层序遍历

二、二叉树遍历递归解析

// 前序遍历·递归·LC144_二叉树的前序遍历
class Solution {public List<Integer> preorderTraversal(TreeNode root) {List<Integer> result = new ArrayList<Integer>();preorder(root, result);return result;}public void preorder(TreeNode root, List<Integer> result) {if (root == null) {return;}result.add(root.val);preorder(root.left, result);preorder(root.right, result);}
}// 中序遍历·递归·LC94_二叉树的中序遍历
class Solution {public List<Integer> inorderTraversal(TreeNode root) {List<Integer> res = new ArrayList<>();inorder(root, res);return res;}void inorder(TreeNode root, List<Integer> list) {if (root == null) {return;}inorder(root.left, list);list.add(root.val);             // 注意这一句inorder(root.right, list);}
}// 后序遍历·递归·LC145_二叉树的后序遍历
class Solution {public List<Integer> postorderTraversal(TreeNode root) {List<Integer> res = new ArrayList<>();postorder(root, res);return res;}void postorder(TreeNode root, List<Integer> list) {if (root == null) {return;}postorder(root.left, list);postorder(root.right, list);list.add(root.val);             // 注意这一句}
}

三、二叉树遍历栈解析

 

// 前序遍历顺序:中-左-右,入栈顺序:中-右-左
class Solution {public List<Integer> preorderTraversal(TreeNode root) {List<Integer> result = new ArrayList<>();if (root == null){return result;}Stack<TreeNode> 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 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;}
}// 后序遍历顺序 左-右-中 入栈顺序:中-左-右 出栈顺序:中-右-左, 最后翻转结果
class Solution {public List<Integer> postorderTraversal(TreeNode root) {List<Integer> result = new ArrayList<>();if (root == null){return result;}Stack<TreeNode> 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 List<List<Integer>> resList = new ArrayList<List<Integer>>();public List<List<Integer>> levelOrder(TreeNode root) {//checkFun01(root,0);checkFun02(root);return resList;}public void checkFun02(TreeNode node) {if (node == null) return;Queue<TreeNode> que = new LinkedList<TreeNode>();que.offer(node);while (!que.isEmpty()) {List<Integer> itemList = new ArrayList<Integer>();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.yayakq.cn/news/666192/

相关文章:

  • 手机网站建设哪儿好沧州做网站哪家好
  • 曹鹏wordpress建站seo视频域名申请到网站上传全过程
  • 怎样给网站做seo优化网站开发 语言 架构 数据库
  • 户县规划建设和住房保障局网站石家庄上门洗车
  • 网站忘记后台地址广东在线网站建设
  • 做电商网站需要多少时间企业查询信息平台
  • 小鱼儿外贸网站网络营销推广公司有哪些
  • 公司网站突然404郑州本地做团购的网站
  • 关于做情侣的网站的图片大全域名注册好了怎么弄网站
  • 做全屏式网站尺寸是多大商丘网站推广
  • 建设银行手机官方网站下载dedecms 营销网站模板
  • 做电脑桌面网站广告手机网站制作
  • 小程序怎么赚钱的seo网站结构
  • 网页制作和网站开发怎样做视频网站的外链
  • 网站后台图片上传失败建设糖果网站的好处有哪些
  • 网站建设轮播图网站个人建设
  • 我用帝国做的网站上传到别一个服务器上重新邦了一个域名做视频怎么做
  • 网站优化基本技巧wordpress tag转专题
  • 何炅做的网站广告批量上传网站产品
  • 网站负责人信息表seo经验
  • 江苏省建设厅网站建造师强制注销怎么查看网站备案号
  • 营销型企业网站建设规划探讨2014最新网站模板-网页模板免费下载-风格吧
  • 对网站排名没有太大影响的因素网站模板化
  • 企业网站事例在哪建设网站
  • 塑胶加工东莞网站建设技术支持怎么用手机创造网站
  • 深圳网站设计成功柚米做影视网站推荐哪个服务器
  • 资讯类网站模板asp为你做的网站
  • 群辉做网站服务器配置虚拟主机如何做网站
  • 漳州专业做网站wordpress能导入多少产品
  • 最好的个人网站建设山东3个中高风险地区