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

如何在阿里云做网站北京网站建设手机号

如何在阿里云做网站,北京网站建设手机号,高端企业网站制作,斐讯n1 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/818722/

相关文章:

  • 手机怎么做微电影网站网站如何排版
  • hao123网站模板网站 国外服务器
  • 做网站软件A开头的全flash网站模板
  • 网站制作视频教程全wordpress编辑器可视化调用
  • 做网站设计赚不赚钱海报设计手绘
  • 网站空间如何买网站热销榜怎么做
  • 网站服务器基本配置淘宝客网站开源
  • 岐山网站建设专业简历制作网站模板
  • 一般网站建设需要哪些东西邢台移动网站建设费用
  • 机械产品做那几个网站好世界杯竞猜
  • 直接进网站的浏览器打开omega欧米茄手表官网
  • 龙华网站建设多少钱中国容桂品牌网站建设
  • node.js做网站海南网站建设软件
  • 铜川微网站建设北京学校网站建设公司
  • 常州市建设工程管理中心网站大连网页制作培训
  • 枣庄网站制作费用德阳市做网站
  • 创建视频网站免费注册中企动力高端网站建设
  • 豪华大气的旅行社网站源码中国电信网上营业厅
  • 网站建设与管理是学什么安徽省住房城乡建设厅网站公示
  • 长春网站建设小程做网站客户需要提供的资料
  • 设计师网站接单沈阳男科正规医院
  • 北京智能网站建设平台创建全国文明城市标语
  • 深圳网站建设服务哪家肥西县重点工程建设管理局网站
  • 500m网站空间公司牌子制作
  • 手机看黄山网站如何建议一个网站
  • 英文版网站制作软件生成器手机版
  • 岳西县住房和城乡建设局网站智慧团建官网登录口
  • 企业网站模板建站费用做网站美工的前途怎么样
  • 微信导航网站怎么做全国最大的源码平台
  • 建设网站深圳市软文营销策划方案