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

400电话安装佛山营销网站建设广告免费设计在线生成

400电话安装佛山营销网站建设,广告免费设计在线生成,怎么做网站流量竞品分析,免费网站推广软件下载大全摘要 博文主要介绍二叉树的前/中/后/层遍历(递归与栈)方法 一、前/中/后/层遍历问题 144. 二叉树的前序遍历 145. 二叉树的后序遍历 94. 二叉树的中序遍历 102. 二叉树的层序遍历 二、二叉树遍历递归解析 // 前序遍历递归LC144_二叉树的前序遍历 class Solution {publi…

摘要

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

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

144. 二叉树的前序遍历

145. 二叉树的后序遍历

94. 二叉树的中序遍历

102. 二叉树的层序遍历

二、二叉树遍历递归解析

// 前序遍历·递归·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/365770/

相关文章:

  • 什么网站做推广比较好17zwd一起做网站官网
  • 当当网网站建设策划书粤语seo是什么意思
  • 为什么做这个网站项目网络系统管理大赛样题
  • 网站如何做才能被360收录h5页面网站模板
  • 网站建设工程师职责说明书有没有找客户的网站
  • 嘉定做网站wordpress 标签 修改
  • 网站建设主要有哪些成本做汽车商城网站
  • 哪几个网站适合自己做外贸做网站的时候说需求的专业术语
  • 西安seo网站排名优化公司企业信用网查询
  • 建好的网站怎么用重庆自助建站模板
  • 哈尔滨市建筑企业管理站营销型手机网站
  • 琳琅秀网站建设网页设计html代码大全居中
  • 深圳公司建站推广永久免费的手机ip代理
  • 网站备案的要求是男的怎么做直播网站
  • 上海嘉定网站十堰吉安营销型网站优化营销
  • 百姓网站外推广怎么做汕头网站推广优化
  • 网站开发项目实训总结体验营销案例
  • 网站qq临时会话怎么弄中国十大公司排名
  • 定制开发响应式网站商城版免费网站
  • 如何做积分商城网站seo北京优化
  • 广州平台网站搭建网站素材大全
  • 公司网站建设济宁网络推广是什么工作
  • 一诺互联 网站建设广州网站推广策划
  • 设计广告公司网站建设网站建设的单可以刷吗
  • 深圳企业网站哪家好网站建设唯地带
  • 网站开发和运行模式的搭建沧州青县机械加工网
  • 邯郸网站设计怎么用哪个学校有网站建设
  • asp 通过ftp 网站搬家摄影网站做画册
  • 广东智慧团建网站登录广州市工商注册查询系统
  • 买什么样的主机(用来建网站的)支持下载常平网站