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

二叉树的遍历总结

  • 144.二叉树的前序遍历(opens new window)
  • 145.二叉树的后序遍历(opens new window)
  • 94.二叉树的中序遍历

二叉数的先中后序统一遍历法

public static  void preOrder(BiTree root){BiTree p = root;LinkedList<BiTree> stack = new LinkedList<>();while(p != null || !stack.isEmpty()){if(p != null){System.out.print(p.val +"\t");stack.push(p);p = p.left;}else {p = stack.pop();p = p.right;}}}
public static  void preOrder00(BiTree root){LinkedList<BiTree>stack = new LinkedList<>();stack.push(root);while(!stack.isEmpty()){BiTree node = stack.peek();if(node != null){stack.pop();if(node.right != null){stack.push(node.right);}if(node.left !=null){stack.push(node.left);}stack.push(node);stack.push(null);}else {stack.pop();BiTree p = stack.peek();stack.pop();System.out.print(p.val + "\t");}}System.out.println();}
public  static void preOrder03(BiTree root){if(root == null){return;}LinkedList<BiTree> stack = new LinkedList<>();stack.push(root);Map<BiTree,Boolean> visited = new HashMap<>();while(!stack.isEmpty()){BiTree node = stack.peek();stack.pop();if(visited.getOrDefault(node,false)){System.out.print(node.val +"\t");continue;}if(node.right != null){stack.push(node.right);visited.put(node.right, false);}if(node.left != null){stack.push(node.left);visited.put(node.left,false);}stack.push(node);visited.put(node,true);}System.out.println("");}

 

public static  void inOrder(BiTree root){LinkedList<BiTree> stack = new LinkedList<>();BiTree p = root;while( p != null || !stack.isEmpty()){if(p != null){stack.push(p);p = p.left;}else {p = stack.pop();System.out.print(p.val + "\t");p = p.right;}}}
 public static void inOrder00(BiTree root){LinkedList<BiTree> stack = new LinkedList<BiTree>();stack.push(root);while(!stack.isEmpty()){BiTree node = stack.peek();if(node != null){stack.pop();if(node.right != null){stack.push(node.right);}stack.push(node);stack.push(null);if(node.left != null){stack.push(node.left);}}else {stack.pop();node = stack.peek();stack.pop();System.out.print(node.val + "\t");}}System.out.println();}
public static void inOrder03(BiTree root){LinkedList<BiTree> stack = new LinkedList<>();stack.push(root);Map<BiTree,Boolean> isVisited = new HashMap<BiTree,Boolean>();while(!stack.isEmpty()){BiTree node = stack.peek();stack.pop();if(isVisited.getOrDefault(node, false)){System.out.print(node.val +"\t");continue;}if(node.right != null){stack.push(node.right);isVisited.put(node.right, false);}stack.push(node);isVisited.put(node, false);if(node.left != null){stack.push(node.left);isVisited.put(node.left, false);}isVisited.put(node, true);}}

 

public static void postOrder00(BiTree root){LinkedList<BiTree> stack = new LinkedList<>();stack.push(root);while(!stack.isEmpty()){BiTree p = stack.peek();if(p != null){stack.pop();stack.push(p);stack.push(null);if(p.right != null){stack.push(p.right);}if(p.left != null) {stack.push(p.left);}}else {stack.pop();p = stack.peek();System.out.print(p.val +"\t");stack.pop();}}System.out.println();}
 public static void postOrder03(BiTree root){LinkedList<BiTree> stack = new LinkedList<>();BiTree p = root;stack.push(p);Map<BiTree,Boolean> isVisited = new HashMap<>();while(!stack.isEmpty()){BiTree node = stack.peek();stack.pop();if(isVisited.getOrDefault(node,false)){System.out.print(node.val+"\t");continue;}stack.push(node);isVisited.put(node, true);if(node.right != null){stack.push(node.right);isVisited.put(node.right, false);}if(node.left != null){stack.push(node.left);isVisited.put(node.left, false);}}}
public static  void postOrder(BiTree root){LinkedList<BiTree> stack = new LinkedList<BiTree>();BiTree p = root;BiTree r = null;while(p != null || !stack.isEmpty()){if(p != null){stack.push(p);p = p.left;}else {p = stack.peek();if (p.right != null && p.right != r) {p = p.right;} else {p = stack.pop();System.out.print(p.val + "\t");r = p;p = null;}}}}

107.二叉树的层次遍历 II

力扣题目链接(opens new window)

给定一个二叉树,返回其节点值自底向上的层次遍历。 (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历)

107.二叉树的层次遍历II

 

 

public List<List<Integer>> levelOrderBottom(TreeNode root) {List<List<Integer>> results = new ArrayList<List<Integer>>();LinkedList<TreeNode> queue = new LinkedList<TreeNode>();if(root == null){return results;}queue.offer(root);while(!queue.isEmpty()){int queueSize = queue.size();List<Integer> result = new ArrayList<Integer>();for(int i = 1; i <= queueSize; i++){TreeNode node = queue.poll();result.add(node.val);if(node.left != null){queue.offer(node.left);}if(node.right != null){queue.offer(node.right);}if(i == queueSize){results.addFirst(result);}}}return results;}

199.二叉树的右视图

力扣题目链接(opens new window)

给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。

199.二叉树的右视图

public List<Integer> rightSideView(TreeNode root) {List<Integer> results=  new ArrayList<Integer>();if(root == null){return results;}Queue<TreeNode> queue  = new LinkedList<TreeNode>();queue.offer(root);while(!queue.isEmpty()){int queueSize = queue.size();for(int i = 1; i <= queueSize; i++){TreeNode node = queue.poll();if(node.left != null){queue.offer(node.left);}if(node.right != null){queue.offer(node.right);}if(i == queueSize){results.add(node.val);}}}return results;}

429.N叉树的层序遍历

力扣题目链接(opens new window)

给定一个 N 叉树,返回其节点值的层序遍历。 (即从左到右,逐层遍历)。

例如,给定一个 3叉树 :

429. N叉树的层序遍历

返回其层序遍历:

[ [1], [3,2,4], [5,6] ]

public List<List<Integer>> levelOrder(Node root) {LinkedList<Node> queue = new LinkedList<Node>();queue.offer(root);List<List<Integer>> results = new ArrayList<List<Integer>>();if(root == null){return results;}while(!queue.isEmpty()){List<Integer> result = new ArrayList<Integer>();int levelSize = queue.size();for(int i = 1; i <= levelSize; i++){Node node = queue.poll();result.add(node.val);if(node.children != null){for(int j = 0; j < node.children.size();j++){queue.offer(node.children.get(j));}}}results.add(result);}return results;}

http://www.lqws.cn/news/160867.html

相关文章:

  • jdbc查询mysql数据库时,出现id顺序错误的情况
  • C:\Users\中文名修改为英文名
  • delphi7 链表 使用方法
  • 性能优化之SSR、SSG
  • 【前端】vue3性能优化方案
  • sourcetree取消待推送
  • 《计算机是怎么跑起来的》第二章读后感
  • 算法题(162):火烧赤壁
  • 13.4 AI颠覆语言学习:预录制视频+GPT-4评估如何实现60%成本降低与40%留存飙升
  • Seata 分布式事务 AT 模式
  • 智慧供水运维管理系统
  • LeetCode 70 爬楼梯(Java)
  • 探索未知惊喜,盲盒抽卡机小程序系统开发新启航
  • 半监督学习:低密度分离假设 (Low-Density Separation Assumption)
  • mysql密码正确SpringBoot和Datagrip却连接不上
  • c++第七天--特殊运算符的重载练习
  • day20 leetcode-hot100-38(二叉树3)
  • 第二章支线八 ·CSS终式:Tailwind与原子风暴
  • 优雅的系统重试
  • 如何轻松将视频从安卓设备传输到电脑?
  • 检测到 #include 错误。请更新 includePath。已为此翻译单元(D:\软件\vscode\test.c)禁用波形曲线
  • 【SSM】SpringMVC学习笔记8:拦截器
  • qt ui 转python
  • YAML在自动化测试中的三大核心作用
  • C++11 尾随返回类型:从入门到精通
  • Linux服务器如何安装wps?
  • 【案例】电商系统的AI微服务架构设计
  • C语言输入函数
  • 使用Python提取照片元数据:方法与实战指南
  • 炫云:为驱动数字视觉产业升级保驾护航