
数据结构
0_o_c
这个作者很懒,什么都没留下…
展开
-
二叉搜索树的后序遍历序列
1、二叉搜索树的后序遍历序列 来源:牛客网 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则输出Yes,否则输出No。假设输入的数组的任意两个数字都互不相同。2、思路:后续遍历中,最后一个元素为根节点,小于根节点的元素在左子树,大于根节点的在右子树。3、代码:public boolean VerifySquenceOfBST(int [] sequence) {原创 2017-09-24 16:34:42 · 267 阅读 · 0 评论 -
由前序和中序数组重建二叉树
1、重建二叉树 来源:牛客网输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。2、思路:先序第一个元素a为根节点,在中序数组中,a左边的元素为左子树,右边为右子树。左右子树放到递归中处理即可3、代码:private原创 2017-09-24 01:38:55 · 292 阅读 · 0 评论 -
把二叉树打印成多行
1、来源:把二叉树打印成多行 从上到下按层打印二叉树,同一层结点从左至右输出。每一层输出一行。2、代码:ArrayList<ArrayList<Integer> > Print(TreeNode pRoot) { ArrayList<ArrayList<Integer> > res = new ArrayList<ArrayList<Integer>>(); Queu原创 2017-09-22 18:22:28 · 233 阅读 · 0 评论 -
二叉树的镜像
1、:链接 来源:牛客网操作给定的二叉树,将其变换为源二叉树的镜像。 输入描述:二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \原创 2017-09-22 14:08:28 · 256 阅读 · 0 评论 -
二叉树的深度
1、题目描述(来自牛客,leetcode也有一样的题目Leetcode-104. Maximum Depth of Binary Tree): 输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。2、思路:动态规划的思想:记子树i的层数为f(i),i 的左右子树分别为left、right则 f(i) = 1+max{f(left原创 2017-09-21 16:31:05 · 282 阅读 · 0 评论 -
recover-binary-search-tree
1、链接:recover-binary-search-tree 来源:牛客网Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing its structure.Note: A solution using O(n ) space is pretty原创 2017-10-17 08:40:39 · 317 阅读 · 0 评论 -
rotate-list
1、来源:rotate-listGiven a list, rotate the list to the right by k places, where k is non-negative. For example: Given1->2->3->4->5->NULLand k =2, return4->5->1->2->3->NULL.2、思路,递归的本质是栈,例子中进栈1、2、3、4、5,原创 2017-10-24 08:55:40 · 257 阅读 · 0 评论 -
climbing-stairs
1、来源:climbing-stairs 来源:牛客网You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?2、思路:直接原创 2017-10-24 00:43:03 · 395 阅读 · 0 评论 -
partition-list
1、来源:partition-list 牛客网Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.You should preserve the original relative order of t原创 2017-10-24 00:31:44 · 331 阅读 · 0 评论 -
binary-tree-zigzag-level-order-traversal
1、来源:binary-tree-zigzag-level-order-traversal牛客网Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alt原创 2017-10-24 00:06:22 · 313 阅读 · 0 评论 -
二叉树中和为某一值的路径
1、来源:二叉树中和为某一值的路径 来源:牛客网输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。 3、思路:先序遍历的方法,当到达叶子节点时判断是否符合要求;保存路径的过程中,用一条公共的list保存每一条路径(保存到二维数组根据该list new一个来保存),一次递归完成后,记得将使用list.re原创 2017-09-24 17:40:00 · 322 阅读 · 0 评论 -
用两个栈实现队列
1、来源:用两个栈实现队列 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。2、代码:public class QueueByStack { Stack<Integer> stack1 = new Stack<Integer>(); Stack<Integer> stack2 = new Stack<Integer>(); public v原创 2017-09-24 23:05:55 · 197 阅读 · 0 评论 -
从尾到头打印链表
1、来源:从尾到头打印链表 输入一个链表,从尾到头打印链表每个节点的值。 2、思路:第一种方法,利用栈的先进后出特性;第二种方法,递归第三种方法,反转链表,还没尝试,待续3、代码:/* * 利用栈来实现 */ /*public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {原创 2017-09-24 23:38:15 · 219 阅读 · 0 评论 -
树的子结构
1、链接:树的子结构 来源:牛客网 [编程题] 输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)2、思路:遍历树,当树的结点值相同时,用root1中以当前结点为根节点的子树与root2对比。3、代码:public class HasSubtree { boolean res = false; public boolean HasSubtr原创 2017-10-01 12:26:06 · 308 阅读 · 0 评论 -
二叉搜索树与双向链表
1、链接:二叉搜索树与双向链表 来源:牛客网 输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的指向。2、思路:中序遍历,遍历的是链表的第一个元素时,构建双向链表;遍历的不是第一个元素时,将该元素放到链表的最后面,然后将指向最后面的endNode移后一位;3、代码:public class TreeConvertLinkList {原创 2017-10-01 11:41:37 · 278 阅读 · 0 评论 -
二叉搜索树的第k个结点
1、链接:二叉搜索树的第k个结点 来源:牛客网 给定一颗二叉搜索树,请找出其中的第k大的结点。例如, 5 / \ 3 7 /\ /\ 2 4 6 8 中,按结点数值大小顺序第三个结点的值为4。2、思路:中序遍历 3、代码:public class KthNode { int pos = 0; TreeNode res = null; boolean isContine原创 2017-09-27 12:49:14 · 355 阅读 · 0 评论 -
对称的二叉树
1、链接:对称的二叉树 来源:牛客网 请实现一个函数,用来判断一颗二叉树是不是对称的。注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的。2、思路:采用递归 3、代码:boolean isSymmetrical(TreeNode pRoot) { boolean res = true; if (pRoot == null) re原创 2017-09-27 12:27:09 · 337 阅读 · 0 评论 -
两个链表的第一个公共结点
1、来源链接:两个链表的第一个公共结点 来源:牛客网 输入两个链表,找出它们的第一个公共结点。2、思路:节点相同,说明它们后面的接着的节点也是一模一样的。另一种思路:求出长度,然后让长的先走长度差。3、代码:public static void main(String[] args){ ListNode list1 = new ListNode(1); ListNo原创 2017-09-27 10:03:18 · 274 阅读 · 0 评论 -
反转链表
1、来源:反转链表 来源:牛客网 输入一个链表,反转链表后,输出链表的所有元素。2、递归,一直深入到链尾,然后又一步步回退到第一步。还有其它方法,待续。 3、代码:public class ReverseList {public static void main(String[] args){ ListNode list1 = new ListNode(1);原创 2017-09-25 18:51:48 · 224 阅读 · 0 评论 -
栈的压入、弹出序列
1、这里写链接内容 来源:牛客网 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。(注意:这两个序列的长度是相等的)2、代码:“` public boolean IsPopOrde原创 2017-09-25 15:27:37 · 234 阅读 · 0 评论 -
合并两个排序的链表
1来源:合并两个排序的链表 来源:牛客网 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。2、代码:public class MergeLink { public static void main(String[] args){ ListNode list1 = new ListNode(1); ListNode t原创 2017-09-25 13:53:17 · 218 阅读 · 0 评论 -
链表中倒数第k个结点
1、来自牛客:链表中倒数第k个结点 输入一个链表,输出该链表中倒数第k个结点。 2、思路,设置两个指针,一个位置为0,另一个位置为k-1,同时进行一步步探测,当在k-1的指针走到末尾时,位置为0的指针为倒数第k的位置。 3、代码:public ListNode FindKthToTail(ListNode head,int k) { int i, pos = k - 1;原创 2017-09-25 01:34:42 · 272 阅读 · 0 评论 -
判断二叉搜索树是否是平衡二叉树
1、判断二叉搜索树是否是平衡二叉树 输入一棵二叉树,判断二叉搜索树是否是平衡二叉树。2、思路:采用递归,算出某次递归左右子树的高度,根据高度差来判断是否满足要求,在递归中有一次高度差大于1时,即不满足。3、代码: public boolean IsBalanced_Solution(TreeNode root) { if(root == null) re原创 2017-09-25 00:43:25 · 860 阅读 · 0 评论 -
construct-binary-tree-from-preorder-and-inorder-traversal
1、来源:construct-binary-tree-from-preorder-and-inorder-traversal Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tr原创 2017-10-22 09:56:58 · 286 阅读 · 0 评论 -
path-sum
1、链接:path-sum 来源:牛客网Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.For example:Given the below bi原创 2017-10-14 14:03:47 · 224 阅读 · 0 评论 -
【面试】排序算法
1、时间和空间复杂度(参考各算法的维基百科绘出)原创 2017-10-13 13:44:00 · 185 阅读 · 0 评论 -
unique-binary-search-trees
1、unique-binary-search-trees 来源:牛客网Given n, how many structurally unique BST's (binary search trees) that store values 1...n?For example,Given n = 3, there are a total of 5 unique BST's. 1原创 2017-10-17 21:07:23 · 268 阅读 · 0 评论 -
108. Convert Sorted Array to Binary Search Tree c++
1、题目:点击打开链接2、给定一个数组,将其转换为一个平衡二叉查找树Given an array where elements are sorted in ascending order, convert it to a height balanced BST.3、代码16ms:/** * Definition for a binary tree node. * struc原创 2016-09-11 02:38:30 · 349 阅读 · 0 评论 -
03-树2. Tree Traversals Again (25)及解题材料
03-树2. Tree Traversals Again (25)时间限制 200 ms内存限制 65536 kB代码长度限制 8000 B判题程序 Standard 作者 CHEN, YueAn inorder binary tree traversal can be implemented转载 2015-04-11 20:56:28 · 688 阅读 · 0 评论 -
ccf最优灌溉最小生成树
201412-4试题名称:最优灌溉时间限制:1.0s内存限制:256.0MB问题描述:问题描述 雷雷承包了很多片麦田,为了灌溉这些麦田,雷雷在第一个麦田挖了一口很深的水井,所有的麦田都从这口井来引水灌溉。 为了灌溉,雷雷需要建立一些水渠,以连接水井和麦田,雷雷也可以利用部分麦田作为“中转站”,利用水渠连接不同的麦田,这样只要一片麦田能被灌溉,则与其连接的麦田也能被灌溉。 现在雷雷知道哪些麦田原创 2015-04-10 11:38:46 · 955 阅读 · 0 评论 -
杭电1233最小生成树kruskal
还是畅通工程Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 28907 Accepted Submission(s): 12924Problem Description某省调查乡村交通状况,得到的统计表中列出了原创 2015-03-31 20:46:56 · 623 阅读 · 0 评论 -
浙大PAT水题简单分析 转
浙大PAT水题简单分析 转(2014-01-25 10:23:19)转载▼标签:it分类:PATPAT(pat.zju.edu.cn)是一个面向C/C++程序的OnlineJudge系统。相比ZOJ,HDOJ,POJ等ACM题库,PAT的题目非常基础,对于数据结构、算法的入门是比较有助益的。本文按照自己的认识,给PAT转载 2015-03-24 16:33:57 · 798 阅读 · 0 评论 -
题目1518:反转链表
题目描述: 输入一个链表,反转链表后,输出链表的所有元素。(hint : 请务必使用链表)输入: 输入可能包含多个测试样例,输入以EOF结束。对于每个测试案例,输入的第一行为一个整数n(0输入的第二行包含n个整数t(0输出: 对应每个测试案例,以此输出链表反转后的元素,如没有元素则输出NULL。样例输入: 51 2 3 4 50样原创 2015-03-24 19:50:35 · 642 阅读 · 0 评论 -
九度1181遍历单链表
题目描述: 建立一个升序链表并遍历输出。输入: 输入的每个案例中第一行包括1个整数:n(1输出: 可能有多组测试数据,对于每组数据,将n个整数建立升序链表,之后遍历链表并输出。样例输入: 43 5 7 9样例输出: 3 5 7 9#include#includeusing namespace std;struct Lis原创 2015-03-23 18:06:47 · 700 阅读 · 0 评论 -
symmetric-tree
1、链接:symmetric-tree来源:牛客网Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree is symmetric: 1 / \ 2 2 / \ / \3 4 4原创 2017-10-17 22:42:21 · 231 阅读 · 0 评论 -
two-sum
1、来源:two-sumGiven an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the ta原创 2017-10-27 11:39:02 · 245 阅读 · 0 评论 -
unique-paths
1、链接:这里写链接内容 A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).The robot can only move either down or right at any point in time. The robot is trying to r原创 2017-10-20 02:25:30 · 237 阅读 · 0 评论 -
construct-binary-tree-from-inorder-and-postorder-traversal
1、链接:这里写链接内容来源:牛客网Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree.2、思路,通过中序和后序来构建树:前序的第一个元素为当前根结点root;在后序中原创 2017-10-22 09:45:14 · 339 阅读 · 0 评论 -
convert-sorted-array-to-binary-search-tree
1、来源:convert-sorted-array-to-binary-search-tree 牛客网Given an array where elements are sorted in ascending order, convert it to a height balanced BST.2、思路:二分查找,注意如果数组的length为偶数,则作为根结点的是右边的元素;当前根结点的左右子结原创 2017-10-22 08:57:04 · 225 阅读 · 0 评论 -
convert-sorted-list-to-binary-search-tree
1、链接:convert-sorted-list-to-binary-search-tree 来源:牛客网Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.2、思路:通过快慢指针找到中间结点,将中间结点设置为空(注意设置该结点为空,需原创 2017-10-21 01:32:57 · 235 阅读 · 0 评论