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

二叉树的遍历总结

  • 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.xdnf.cn/news/12305.html

相关文章:

  • 统信桌面专业版如何使用python开发平台jupyter
  • Kotlin 2.1 一元二次方程(顺序结构版)
  • three.js中使用tween.js的chain实现动画依次执行
  • 第09期_网站搭建_卡密验证 易如意1.71正式版 虚拟主机搭建笔记 软件卡密系统
  • 嵌入式学习 D33:系统编程--网路编程
  • dvwa12——XSS(Stored)
  • 回文数 - 力扣
  • Vue Router的核心实现原理深度解析
  • Python训练营打卡 Day45
  • RAID磁盘阵列
  • 算法:前缀和
  • 动态规划---股票问题
  • Job 运维类
  • JAVA数据库连接
  • Rocketmq消息队列 消息模型 详解
  • [蓝桥杯]全球变暖
  • Filebeat收集nginx日志到elasticsearch,最终在kibana做展示(二)
  • Next-AI聊天应用-复用chat组件
  • 数据炼金术:电商突围的智能决策革命
  • [闭源saas选项]Pinecone:为向量数据库而生的实时语义搜索引擎
  • OMS主动运维服务:赋能中小企业运维价值升级
  • Java类加载过程
  • 使用子树合并策略更新git项目的部分目录
  • ignore文件不生效的问题
  • 初识硬编码(x86指令描述)
  • 代码随想录算法训练营第九天| 151.翻转字符串里的单词、55.右旋转字符串 、字符串总结
  • CLIP多模态大模型的优势及其在边缘计算中的应用
  • 实时云渲染解决UE像素流送无法进行二次开发的问题
  • spring注解之配置注解
  • 《图解技术体系》How Redis Architecture Evolves?