![](https://img-blog.csdnimg.cn/20201014180756724.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
算法
sun_jinhang
这个作者很懒,什么都没留下…
展开
-
求二叉树的最大深度的递归版与非递归版
二叉树的的最大深度遍历非递归版描述:给定一个二叉树,返回二叉树的最大深度思路:利用队列的先进先出思想对二叉树进行层级遍历,每遍历完一层,就将深度+1具体实现:1、首先判断二叉树是否为空,如果为空,直接 return 02、如果不为空,首先将根节点入队,然后对队列进行遍历3、初始化三个变量 depth 深度,count 计数器,nextCount 层级节点数,这里就是利用count和ne...原创 2019-03-25 14:04:14 · 712 阅读 · 0 评论 -
二叉树的前序遍历、中序遍历、后序遍历
二叉树分为根节点、左子节点、右子节点,如下图所示,前序遍历的顺序是 “根左右”,即 ABC,中序遍历的顺序是 “左根右”,即 BAC,后续遍历的顺序是 “左右根”,即 BCA,给定一个二叉树,求三种遍历方式的结果,以下图为例前序遍历(根左右): A B D E F G C H K中序遍历(左根右): D B F E G A C K H后序遍历(左右根):...原创 2019-04-04 15:23:10 · 668 阅读 · 0 评论 -
Java 使用单重For循环找出数组中第二大数
前几天遇到了一个很有意思的题目:给定一个整形数组,长度不定,如何使用一个for循环得到数组中第二大的数字。下面是我的解法:public static int FindSecondMax(int[] array) { int max,max2; if(array.length < 2) { return null; } max = max2 = -2147...原创 2019-06-18 17:45:09 · 2187 阅读 · 0 评论 -
冒泡排序及其优化
冒泡排序 基础版public static int[] BubbleSort(int[] array) { for(int i = 0;i < array.length;i++) { for(int j = 0;j<array.length -1;j++) { if(array[j] > array[j+1]) { int temp = arra...原创 2019-06-19 10:41:01 · 118 阅读 · 0 评论 -
选择排序及其优化
选择排序基础版public static int[] SelectSort(int[] array) { for(int i = 0;i < array.length; i++) { int index = i; for(int j = i; j < array.length ;j++) { if(array[j] < array[index]) ...原创 2019-06-19 14:05:37 · 318 阅读 · 0 评论