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

免费下载微信并安装谷歌seo代运营

免费下载微信并安装,谷歌seo代运营,亚马逊网网站建设规划报告,创意福州网站建设摘要 博文主要介绍二叉树的前/中/后/层遍历(递归与栈)方法 一、前/中/后/层遍历问题 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.mmbaike.com/news/94378.html

相关文章:

  • 网站加搜索框上海网站seo外包
  • 做漫画在线观看网站搜狗网址
  • 怎样可以做网站站长2023年适合小学生的新闻
  • 住建网站需多少钱网络营销的网站建设
  • 合肥定制网站建设关键词优化一般收费价格
  • 太原网站制作价格seo查询是什么意思
  • php网站开发外文互联网app推广具体怎么做
  • 青州做网站电话重庆百度推广排名优化
  • 廉政网站管理制度建设网络营销考试答案
  • 做网站江西十大新媒体平台有哪些
  • 在线学做网站百度营销登录
  • 德格网站建设淘宝搜索词排名查询
  • 上行10m企业光纤做网站身边的网络营销案例
  • 网站域名spaceseodao cn
  • 网站建设免费百度在线客服
  • 网站建设建设汕头网站建设方案开发
  • 给私人企业做网站推广软文代发价格
  • 网站建设发布全网关键词搜索工具
  • 自己如何制作一个软件seo站外优化最主要的是什么
  • 怎么做一元抽奖网站北京网络营销招聘
  • 做海报图片的网站佛山百度快速排名优化
  • 建网站的策划方案网络营销策划方案框架
  • 记的网站域名广告营销案例分析
  • 电子商城网站建设方案互联网媒体广告公司
  • 公司简单网站多少钱培训机构管理系统
  • 游戏网站风控怎么做seo搜索引擎优化推荐
  • 做外贸自己开公司网站磁力屋torrentkitty
  • 网站seo报表百度网盘网页版登录
  • 昆明网站建设 昆明光硕友情链接检测
  • 怎样做网站搜索推广重庆百度seo排名