![](https://img-blog.csdnimg.cn/20201014180756919.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
数据结构算法
Vince_Wang1
What hurts more? The pain of hard-working or the pain of regret?
展开
-
二叉树的最小深度踩坑
今天做题的时候做到leetcode 111,二叉树最小深度。看了一眼以为可以秒杀(我太天真了)写下如下代码 return root == null ? 0 : Math.min(minDepth(root.left),minDepth(root.right))+1;然后。。。gg了原因是当存在这样左子树或者柚子树为空的情况下,使用如上代码输出为1,但1是有子节点的,所以不符合最小深...原创 2020-03-16 10:36:02 · 183 阅读 · 1 评论 -
使用非递归方式遍历二叉树
补数据结构算法中,发现Leetcode也有遍历二叉树的习题,特来码一下前序创建辅助栈,每次迭代弹出根节点,并按照右 左的顺序压入栈,故出栈时为,根 左 右,符合前序遍历class Solution { public List<Integer> preorderTraversal(TreeNode root) { Stack<TreeNode> ...原创 2020-02-07 16:32:25 · 268 阅读 · 0 评论 -
插入排序和希尔排序
为了复习直接在网页码的 格式欠佳插入排序public static void sort(int a[]){int j;for(int p=1;p<a.length;p++){ int temp=a[p]; for(j=p;j>0&&temp<a[j-1];j--) a[j]=a[j-1]; a[j]=temp; }}...原创 2020-02-06 14:12:15 · 151 阅读 · 0 评论 -
快速排序和堆排序
快排public class QuickSort { public static int[] Sort(int []a,int left,int right){ if(left<right){ int mid=position(a,left,right);//获取中间值 a=Sort(a,left,mid-1);//中间值...原创 2020-02-06 11:10:35 · 197 阅读 · 0 评论 -
KMP模式匹配算法
在知道这个之前…原来我一直在暴力for循环。。。。真是不知者无畏啊暴力匹配就不说了 大家都知道怎么回事 无非是挨个比,如果不匹配了,就像后挪一位再比,以此类推,效率极低。在最坏情况下的时间复杂度相当高引用于大话数据结构...原创 2020-01-27 10:14:08 · 635 阅读 · 0 评论