更换map的遍历顺序优先级

In this tutorial, we’ll be discussing what’s Level Order Traversal. Also, we’ll be printing each level of the tree. We’ll use the two major approaches – Recursive and Iterative and see what’s the difference in time complexities between them.

在本教程中,我们将讨论什么是水平订单遍历。 另外,我们将打印树的每个级别。 我们将使用两种主要方法-递归和迭代,看看它们之间的时间复杂度有何不同。

级别顺序遍历 (Level Order Traversal)

Level Order traversal is also known as Breadth-First Traversal since it traverses all the nodes at each level before going to the next level (depth).

级别顺序遍历也称为广度优先遍历,因为它在进入下一个级别(深度)之前遍历每个级别的所有节点。

Following illustration shows levels of a Binary Tree:

下图显示了二叉树的级别:

The last level of the tree is always equal to the height of the tree.

树的最后一层始终等于树的高度。

The last level of the tree should contain at least one Node.

树的最后一级应至少包含一个节点。

Following is the class that defines the Binary Tree Data Structure:

以下是定义二叉树数据结构的类:

public class BinaryTree {public TreeNode root;public static class TreeNode {public TreeNode left;public TreeNode right;public Object data;public TreeNode(Object data) {this.data = data;left = right = null;}}
}

Let’s write the Java Program for iterative and recursive approaches for breadth-first traversal.

让我们为广度优先遍历的迭代和递归方法编写Java程序。

广度优先遍历–递归 (Breadth First Traversal – Recursively)

Following Java program uses the recursive approach to print each level of the tree:

以下Java程序使用递归方法来打印树的每个级别:

package com.journaldev.tree.levelOrderTraversal;import com.journaldev.tree.height.BinaryTree;
import com.journaldev.tree.height.BinaryTree.TreeNode;public class PrintLevelsOfTree {public static void main(String[] args) {BinaryTree binaryTree = new BinaryTree();/*** Binary Tree in our example, height = 2*       1       (Root)*       2   3     (Level 1)*      4       5       (Level 2)*/binaryTree.root = new BinaryTree.TreeNode(1);binaryTree.root.left = new BinaryTree.TreeNode(2);binaryTree.root.right = new BinaryTree.TreeNode(3);binaryTree.root.left.left = new BinaryTree.TreeNode(4);binaryTree.root.right.left = new BinaryTree.TreeNode(5);printLevelsRecursively(binaryTree.root);}private static void printLevelsRecursively(TreeNode root) {int height = heightOfTree(root);for (int i = 1; i <= height; i++) {System.out.print("Level " + i + " : ");printSingleLevelRecursively(root, i);System.out.println();}}private static int heightOfTree(TreeNode root) {if (root == null) {return 0;}int leftHeight = heightOfTree(root.left);int rightHeight = heightOfTree(root.right);return Math.max(leftHeight, rightHeight) + 1;}private static void printSingleLevelRecursively(TreeNode root, int level) {if (root == null)return;if (level == 1) {System.out.print(root.data + " ");} else if (level > 1) {printSingleLevelRecursively(root.left, level - 1);printSingleLevelRecursively(root.right, level - 1);}}
}

Following is the output that gets printed:

以下是输出的输出:

Level Order Complexity for Recursive Approach递归方法的层级顺序复杂度

Time Complexity – O(n^2)
Space Complextiy – O(1)

时间复杂度– O(n ^ 2)
空间复杂度– O(1)

Let’s optimize the above program by using the Iterative approach instead.

让我们通过使用迭代方法来优化上述程序。

public static void printLevelsIteratively(TreeNode root) {Queue<TreeNode> queue = new LinkedList<>();queue.add(root);while (!queue.isEmpty()) {TreeNode node = queue.peek();if (node != null) {System.out.print(node.data + " ");queue.remove();if (node.left != null)queue.add(node.left);if (node.right != null)queue.add(node.right);}}}

This prints the following:

打印以下内容:

In the above code, each Node is enqueued and dequeued once.

在上面的代码中,每个节点入队和出队一次。

Level Order Complexity for Iterative Approach迭代方法的层级顺序复杂度

Time Complexity – O(n)
Space Complextiy – O(n)

时间复杂度– O(n)
空间复杂度– O(n)

The Queue size is equal to the number of the nodes.

队列大小等于节点数。

So in the above approach, we’ve traded with some space for optimizing the approach.

因此,在上述方法中,我们已经为优化该方法付出了一些空间。

That brings an end to this tutorial.

这样就结束了本教程。

GitHub Repository.GitHub存储库中检出完整的代码以及更多DS和算法示例。

翻译自: https://www.journaldev.com/23059/level-order-traversal-breadth-first-tree

更换map的遍历顺序优先级

更换map的遍历顺序优先级_树的级别顺序遍历或宽度优先遍历相关推荐

  1. 图的深度优先遍历和宽度优先遍历C语言,图的广度、深度优先遍历 C语言

    以下是老师作为数据结构课的作业的要求,没有什么实际用处和可以探讨和总结的的地方,所以简单代码直接展示. 宽度优先遍历: #include #include #include using namespa ...

  2. 有苦有乐的算法 --- 图的宽度优先遍历

    题目 给定一个图,使用队列对其进行宽度优先遍历 代码 public static void bfs(Node start) {if (start == null) {return;}Queue< ...

  3. 进栈顺序为abcd则出栈顺序为_线性表之顺序表示

    线性表 1. 线性表的基础知识 1.1 线性表的定义 线性表是具有相同数据类型的n(n>0)个数据元素的有限序列. 若用L命名,表示:L=(a1,a2,a3,-,ai-1,ai,ai+1,-,a ...

  4. 二叉树层次遍历c语言_每日一道 LeetCode (23):二叉树的层次遍历 II

    ❝ 每天 3 分钟,走上算法的逆袭之路. ❞ 前文合集 每日一道 LeetCode 前文合集 代码仓库 GitHub:https://github.com/meteor1993/LeetCode Gi ...

  5. 送外卖优先级_如何在恶劣天气让外卖小哥优先送您的订单?

    当下城市的生活节奏已经越来越快,越来越多的人已经习惯了叫外卖,大多数情况下,顾客的外卖都会准时准点送到顾客手中,然而恶劣天气,包括三伏天三九天,下雨下雪,(这里不包含自然灾害以及台风这种极端天气,因为 ...

  6. 宽度优先遍历(BFS)

    BFS邻接矩阵描述 #include <stdio.h> #include <malloc.h> #define VERTEXNUM 5 //队列的元素 typedef str ...

  7. C++笔记-二维棋盘数组使用BFS(宽度优先遍历)

    这里只对一个顶点只能上下左右,不能和左上,左下,右上,右下连起来. 思路步骤: 1.二维棋盘数据转链接表: 2.邻接表直接进行BFS 源码如下: #include <QDebug> #in ...

  8. 二叉树层序遍历(宽度优先遍历bfs--队列)

    https://leetcode-cn.com/problems/cong-shang-dao-xia-da-yin-er-cha-shu-ii-lcof/solution/mian-shi-ti-3 ...

  9. C++笔记-基于邻接矩阵的BFS(宽度优先遍历)

    邻接表是是用一个二维链表,表示顶点和顶点相邻的节点. 而邻接矩阵是,他的行,代表的是顶点,列也代表的顶点,如下: 第0行:这是第0个顶点,他可以去第1个和第2个顶点: 第1行:这是第1个顶点,他可以去 ...

最新文章

  1. 前端一HTML:十六: 权重有关的练习
  2. 1086 Tree Traversals Again (25 分)【一般 / 建树 树的遍历】
  3. TensorFlow索引与切片语句
  4. UI 设计:如何做到理性?
  5. ABAP知识:LIKE LINE OF 和LIKE TABLE OF
  6. vue项目中运用webpack动态配置打包多种环境域名
  7. hutool 自定义excel_Hutool Java 工具类库导出 Excel,超级简单!
  8. MyBatis 在xml文件中处理大于号小于号的方法
  9. 使用mysqldump 导出sql数据
  10. 势在人为:人才吸引力报告2020
  11. easyui input输入框的限制和校验条件
  12. 用牛顿插值多项式求函数近似值的算法,能用C语言编程实现
  13. asp.net 获取访问的url
  14. poj 1833 排列
  15. 微服务面试题及详细答案
  16. 测量图上点位平面位置\高程中误差怎么计算?
  17. .NET Core剪裁器背后的技术
  18. 【Spring】注解驱动开发
  19. Apple个人开发者账号相关问题
  20. 毕业设计(论文)的内容要求和参考格式

热门文章

  1. weblogic8.1在myeclipse中启动正常,在单独的weblogic中无法正常启动的解决方案.
  2. 《大型网站技术架构》读书笔记[3] - 架构核心五要素
  3. 如何对ASP.NET进行性能优化
  4. [转载] python中bool啥意思_Python中的bool类型
  5. [转载] python中chr和str,以及ordint
  6. [转载] numpy数组遍历找到个数最多的元素
  7. spring缓存与Redis
  8. jQueryEasyUI应用 – datagrid之CRUD应用
  9. nopcommerce笔记3 还可以控制什么
  10. 公司有内部推荐的名额