数据结构-二叉树的遍历

devtools/2024/10/18 12:43:14/

二叉树的遍历广义上是指下面我们说的七种遍历

深度优先搜索 : 递归完成 前序 中序 后序 的遍历
广度优先搜索 : 层序遍历(借助队列)
非递归的迭代法完成前中后遍历(借助栈)

代码合集如下

package TreeDemo;
import java.util.*;
public class BinaryTreeTest {public static class TreeNode {int val;TreeNode left;TreeNode right;public TreeNode(int val) {this.val = val;}}/*** 下面的几种方法分别是* 前中后序的递归遍历(又称深度优先搜索)* 前中后序的迭代法遍历(用栈去模拟)* 层序遍历(广度优先搜索 --> 用队列模拟)*///前序遍历的递归法List<Integer> list1 = new ArrayList<>();public List<Integer> preOrder(TreeNode root) {if (root == null) {return list1;}//进入递归环节list1.add(root.val);preOrder(root.left);preOrder(root.right);return list1;}//前序遍历的非递归写法(迭代法)private Stack<TreeNode> stack1 = new Stack<>();private List<Integer> list2 = new ArrayList<>();public List<Integer> preOrderUseWhile(TreeNode root) {if (root == null) {return list2;}stack1.push(root);while (!stack1.isEmpty()) {TreeNode node = stack1.pop();list2.add(node.val);if (node.right != null) {stack1.push(root.right);}if (node.left != null) {stack1.push(root.left);}}return list2;}//中序遍历的递归法private List<Integer> list3 = new ArrayList<>();public List<Integer> inOrder(TreeNode root) {if (root == null) {return list3;}inOrder(root.left);list3.add(root.val);inOrder(root.right);return list3;}//中序遍历的迭代法private List<Integer> list4 = new ArrayList<>();private Stack<TreeNode> stack2 = new Stack<>();public List<Integer> inOrderUseWhile(TreeNode root) {if (root == null) {return list4;}TreeNode cur = root;//这里的逻辑判断要重视一下啊,这个条件的逆命题是栈为空同时指针也为空...while (!stack2.isEmpty() || cur != null) {if (cur != null) {stack2.push(cur);cur = cur.left;} else {TreeNode node = stack2.pop();list4.add(node.val);cur = node.right;}}return list4;}//后序遍历的递归法private List<Integer> posOrderList = new ArrayList<>();public List<Integer> posOrder(TreeNode root) {if (root == null) {return posOrderList;}posOrder(root.left);posOrder(root.right);posOrderList.add(root.val);return posOrderList;}//后续遍历的迭代法private List<Integer> posOrderlist = new ArrayList<>();private Stack<TreeNode> posOrderStack = new Stack<>();public List<Integer> posOrderUseWhile(TreeNode root) {if (root == null) {return posOrderlist;}posOrderStack.push(root);while (!posOrderStack.isEmpty()) {TreeNode node = posOrderStack.pop();posOrderlist.add(node.val);if (node.left != null) {posOrderStack.push(node.left);}if (node.right != null) {posOrderStack.push(node.right);}}//反转顺序表int size = posOrderlist.size();int left = 0;int right = size - 1;while (left <= right) {Integer temp = posOrderlist.get(left);posOrderlist.set(left, posOrderlist.get(right));posOrderlist.set(right, temp);left++;right--;}return posOrderlist;}/*** 二叉树的层序遍历,运用的是队列的思想* 核心逻辑就是利用队列的大小来定向的控制移动元素的数量从而达到层序遍历的效果* 也就是我们图论中的广度优先搜索...*/public List<List<Integer>> levelList = new ArrayList<>();public Queue<TreeNode> queue = new LinkedList<>();public List<List<Integer>> levelOrder(TreeNode root) {if(root == null){return levelList;}queue.offer(root);while(!queue.isEmpty()){int size = queue.size();List<Integer> tempList = new ArrayList<>();while(size-- != 0){TreeNode node = queue.poll();tempList.add(node.val);if(node.left != null){queue.offer(node.left);}if(node.right != null){queue.offer(node.right);}}levelList.add(tempList);}return levelList;}
}

层序遍历的应用 --> 补档"前世今生"

/*** 今天我们正式用树的观点解决一下前世今生*/
class PreLiveAndCur {public static class TreeNode {int val;TreeNode left;TreeNode right;public TreeNode() {}public TreeNode(int val) {this.val = val;}}/*** 这个方法的作用是用递归尝试生成一颗满二叉树* @param level : 层数* @return*/public static TreeNode getFullTree(int level) {if (level == 1) {return new TreeNode(2);}TreeNode root = new TreeNode(2);root.left = getFullTree(level - 1);root.right = getFullTree(level - 1);return root;}/*** 不用计数的方式,利用我们的层序迭代遍历来初始化(广度优先搜索)* 由于我们这玩意百分百是一个满二叉树,也就是我们的size一定为2的倍数* @param root* @param level*/private static Queue<TreeNode> queue = new LinkedList<>();public static void initLevelValue(TreeNode root, int level) {if(root == null){throw new RuntimeException("不是哥们...");}int countMax = (int) Math.pow(2, level - 1);queue.offer(root);boolean flag = false;int value = 1;while(!queue.isEmpty()){int size = queue.size();if(size == countMax){flag = true;}while(size-- != 0){TreeNode node = queue.poll();if(flag){node.val = value++;}if(node.left != null){queue.offer(node.left);}if(node.right != null){queue.offer(node.right);}}}}/*** 根据传入的根节点与字符串(注意传入的两个参数要进行匹配)* @param s : 字符串* @param root : 根节点的坐标* @return*/public static int getContext(String s,TreeNode root){TreeNode cur = root;for(int i = 0; i < s.length(); ++i){char tempChar = s.charAt(i);if(tempChar == 'y'){cur = cur.left;}else{cur = cur.right;}}return cur.val;}//创建一个集合类链表来接收返回数据private static List<Integer> res = new LinkedList<>();public static void main(String[] args) {Scanner sc = new Scanner(System.in);int problemNumber = sc.nextInt();int personNumber = sc.nextInt();int level = problemNumber + 1;TreeNode root = getFullTree(level);initLevelValue(root,level);for(int i = 0; i < personNumber; ++i){String s = sc.next();res.add(getContext(s,root));}for(Integer elem : res){System.out.println(elem);}}
}

http://www.ppmy.cn/devtools/28317.html

相关文章

vue3中reactive和ref的比较

reactiveref❌ 只支持对象和数组&#xff08;引用数据类型&#xff09;✅ 支持基本数据类型 引用数据类型✅ 在 <script> 和 <template> 中无差别使用✅ 支持基本数据类型 引用数据类型❌ 重新分配一个新对象会丢失响应性✅ 重新分配一个新对象不会失去响应能直接…

第一篇:Python简介:开启你的编程之旅

Python简介&#xff1a;开启你的编程之旅 在这个系列文章中&#xff0c;我将带领大家深入了解Python——一个极具魅力的编程语言。如果你对编程感兴趣&#xff0c;想要掌握一门既实用又强大的语言&#xff0c;那么Python无疑是一个绝佳的选择。本篇文章是这个系列的序章&#…

【刷爆力扣之637. 二叉树的层平均值】

637. 二叉树的层平均值 方法一&#xff1a;深度优先搜索dfs 使用深度优先搜索计算二叉树的层平均值&#xff0c;需要维护两个数组&#xff0c;counts 用于存储二叉树的每一层的节点数&#xff0c;sums 用于存储二叉树的每一层的节点值之和。搜索过程中需要记录当前节点所在层…

智慧旅居养老系统最大的品牌,旅居养老系统哪个好用

为了提供更加舒适、便捷、安全的养老服务&#xff0c;我们行心科技的智慧旅居养老系统应运而生。该系统结合了物联网、大数据、人工智能等先进技术&#xff0c;为老年人提供全方位的居家养老服务。 该旅居养老系统系统通过集成各种智能设备和服务&#xff0c;包括健康筛查与评…

数据结构知识点

目录 一、顺序表 1、顺序表插入删除&#xff1a; 二、链表 1、单链表 1.1、插入 具体操作 1.2、删除 2、循环链表 2.1、双向循环链表判空 2.2、双向循环插入 链表和顺序表的区别&#xff1a; 三、栈 3.1、链栈与顺序栈区别 3.2、用栈模拟队列 注意事项&#xff1a…

C# WinForm —— 11 ListBox介绍

1. 简介 列表框&#xff0c;可以从中选择一项或多项 2. 常用属性 属性解释(Name)控件ID&#xff0c;在代码里引用的时候会用到,一般以 lb开头BackColor背景颜色BoderStyle边框样式&#xff1a;无、FixedSingle、Fixed3DDataSource指示此控件将用来获取其项的列表&#xff0…

Spring Cloud——LoadBalancer

Spring Cloud——LoadBalancer 一、负载均衡&#xff08;LoadBalance&#xff09;1.LoadBalancer本地负载均衡客户端 VS Nginx服务端负载均衡区别 二、LoadBalancer1.Spring RestTemplate as a LoadBalancer Client2.编码使用DiscoveryClient动态获取所有上线的服务列表3.从默认…

附录3-小程序常用事件

目录 1 点击事件 tap 2 文本框输入事件 input 3 状态改变事件 change 4 下拉刷新事件 onPullDownRefresh() 5 上拉触底事件 onReachBottom() 1 点击事件 tap 2 文本框输入事件 input 可以使用 e.detail.value 打印出当前文本框的值 我现在在文本框中依次输入12345&…