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

数据结构---二叉树

二叉树的举例

遍历

广度优先遍历

广度优先遍历(Breadth-first-order):尽可能先访问距离根最近的节点,也称为层序遍历

练习
leetCode102
思路
/*1/   \2     3/   \  /  \4    5 6    7头[]尾1 2 3 4 5 6 7
*/
实现
public class E01Leetcode102 {public static void main(String[] args) {TreeNode root = new TreeNode(new TreeNode(new TreeNode(4),2,new TreeNode(5)),1,new TreeNode(new TreeNode(6),3,new TreeNode(7)));
​System.out.println(testMethod(root));Queue<TreeNode> queue = new LinkedList<>();}public static List<List<TreeNode>> testMethod(TreeNode root){if(root == null){return null;}List<List<TreeNode>> list = new ArrayList<>();LinkedListQueue<TreeNode> queue = new LinkedListQueue();queue.offer(root);int index = 1;while (!queue.isEmpty()){int temp = 0;List<TreeNode> leve = new ArrayList<>();for(int i = 0; i < index; i++){TreeNode n = queue.poll();leve.add(n);if(n.left != null){queue.offer(n.left);temp++;}if(n.right != null){queue.offer(n.right);temp++;}}index = temp;list.add(leve);}return list;}
}
深度优先遍历

规则

1.深度优先遍历(Depth-first order):对于二叉树,可以进一步分成三种(要深入到叶子节点)

前序遍历:先访问该节点,然后是左子树,最后是右子树

中序遍历:先访问左子树,然后是该节点,最后是右子树

后序遍历:先访问左子树,然后是右子树,最后是该节点

实现

前序遍历

LinkedList<TreeNode> list = new LinkedList();
TreeNode curr = root;
​
while(curr != null || !list.isEmpty){if(curr != null){System.out.println(curr.val);list.push(curr);curr = curr.left;}else{TreeNode pop = list.pop();curr = pop.left}
}

中序遍历

LinkedList<TreeNode> list = new LinkedList();
TreeNode curr = root;
​
while(curr != null || !list.isEmpty){if(curr != null){list.push(curr);curr = curr.left;}else{TreeNode pop = list.pop();curr = pop.leftSystem.out.println(pop.val);}
}

后续遍历

    static void preOrderEst2(TreeNode node){TreeNode curr = node;LinkedList<TreeNode> list = new LinkedList<>();TreeNode pop = null;while(curr != null || !list.isEmpty()){if(curr != null){list.push(curr);curr = curr.left;}else{TreeNode peek = list.peek();if(peek.right == null || pop == peek.right){pop = list.pop();System.out.println("回" + pop.val);}else {curr = peek.right;}}}}

同时解决所有深度优先遍历

    static void preOrderEst3(TreeNode node){TreeNode curr = node;LinkedList<TreeNode> list = new LinkedList<>();TreeNode pop = null;while(curr != null || !list.isEmpty()){//待处理的左子树if(curr != null){list.push(curr);System.out.println("前" + curr.val);curr = curr.left;}else{//右子树为空的时候TreeNode peek = list.peek();if(peek.right == null){System.out.println("中" + peek.val);pop = list.pop();System.out.println("后" + pop.val);//右子树处理完毕}else if(pop == peek.right){pop = list.pop();System.out.println("后" + pop.val);// 待处理的右子树}else{System.out.println("中" + peek.val);curr = peek.right;}}}}

http://www.xdnf.cn/news/8676.html

相关文章:

  • 基于python的机器学习(九)—— 评估算法(二)
  • OpenLayers 开发环境搭建
  • 初识 RocketMQ 知识总结:基础概念、架构解析、核心特性与应用场景
  • 解决“uv 无法识别为命令”问题:Windows 下 Python 工具安装后的路径配置方法
  • 国际前沿知识系列五:时间序列建模方法在头部撞击运动学测量数据降噪中的应用
  • Spring Cloud Gateway 微服务网关实战指南
  • python操作MySQL数据库
  • 再论自然数全加和-4
  • laravel中模型中$fillable的用法
  • DeepSeek-R1 模型现已在亚马逊云科技上推出
  • 再谈Linux进程:进程等待、进程替换与环境变量
  • 第七章:组件之城 · 重构世界的拼图术
  • RabbitMQ 快速上手
  • 【RichTextEditor】 【分析2】RichTextEditor设置文字内容背景色
  • 第八章:数据库查询优化
  • 上升沿计数 stm32 中断
  • 用service 和 SCAN实现sqlplus/jdbc连接Oracle 11g RAC时负载均衡
  • 在Mac中使用pyenv管理Python版本:从安装到虚拟环境的全流程指南
  • 物联网网关保障沼气发电站安全运行的关键技术解析
  • 文章记单词 | 第111篇(六级)
  • 江科大ADC模数转换hal库实现
  • C++构造函数和析构函数
  • 静态库的使用方法
  • BaseDao指南
  • 生成模型——变分自动编码器(Variational Autoencoders, VAEs)
  • 项目管理进阶:111页 详解华为业务变革框架及战略级项目管理【附全文阅读】
  • LaTeX学习路线
  • 63. 不同路径 II
  • 2.2.1 05年T1复习
  • 1.2 TypeScript 与 JavaScript 的区别