
ACM-树与图论
文章平均质量分 76
LarryNLPIR
专注NLP/IR/Machine Learning/Data Mining
展开
-
POJ 2255 根据二叉树的前序和中序序列来重建二叉树
根据二叉树的前序和中序序列来重建二叉树,输出其后序序列这是面试笔试中经常遇到的问题关键要理解再先序和后序序列中找左右子树的先序和后序序列的方法,利用递归实现#include #include using namespace std;struct TreeNode{ char val; TreeNode *left,*right;};TreeNode * bulidTree(string pre,string in){ TreeNode * root =NULL; if (原创 2010-11-16 18:05:00 · 2772 阅读 · 0 评论 -
LeetCode 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 tree.思路分析:这题类似于LeetCode Construct Binary Tree from Inorder and Postord原创 2015-01-28 12:30:19 · 2174 阅读 · 1 评论 -
POJ 3522 变形kruskal算法及并查集的实现
此题对kruskal算法做了变形,不是求最小生成树,而是求最大边权值与最小边权值之差最小的生成树,同样可以用kruskal算法的实现方法,采用并查集。如果求最小生成树要将边加入到堆中,并且不需要遍历所有的生成树情况//此题对kruskal算法做了变形,不是求最小生成树//而是求最大边权值与最小边权值之差最小的生成树//同样可以用kruskal算法的实现方法,采用并查集#include #include #include #define inf 10000000//边权值的上界us原创 2010-11-23 21:02:00 · 1921 阅读 · 0 评论 -
LeetCode Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.思路分析:这题比较简单,关于树的题目通常都可以用递归解决,这题也不例外,递归解法的思原创 2015-03-19 03:50:24 · 1668 阅读 · 0 评论 -
LeetCode Same Tree
Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the same value.思路分析:判断两个树是否相同,基本也原创 2015-03-19 04:04:40 · 1592 阅读 · 0 评论 -
LeetCode Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.思路分析:这题和Maximum Depth of Binary Tree类似,但是现原创 2015-03-19 13:25:30 · 1473 阅读 · 0 评论 -
LeetCode Clone Graph
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.OJ's undirected graph serialization:Nodes are labeled uniquely.We use # as a separator for each node, and原创 2015-03-29 12:36:21 · 1784 阅读 · 0 评论 -
LeetCode Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3, 4]. Re原创 2015-03-22 12:08:06 · 1684 阅读 · 0 评论 -
LeetCode Implement Trie (Prefix Tree)
Implement a trie with insert, search, and startsWith methods.Note:You may assume that all inputs are consist of lowercase letters a-z.思路分析:这题主要考察Trie 即前缀树的实现,Trie可以用于字典的压缩存储,可以节省空间,但是不节省时间(和HashSet相比)原创 2015-05-31 12:30:46 · 6038 阅读 · 1 评论 -
LeetCode Binary Tree Right Side View
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.For example:Given the following binary tree, 1原创 2015-05-25 14:37:25 · 2255 阅读 · 0 评论 -
LeetCode Invert Binary Tree
这是最近比较火的一个题目,因为一条推特的转播“Google HR:我们90%的工程师都用你写的软件,但是你竟然不会在白板上面反转一颗二叉树,所以滚吧”,各方看法不一,但看这个题目,的确是一个很简单的题目。考察最基本的递归和树操作。下面给出了递归实现和借助栈的迭代实现(非递归实现)。后一个版本的可扩展性更好,可以处理更大的树。实在是容易题,就是DFS遍历一遍树节点,把每个树节点的左右孩子互换就可以了。或许是那个ios开发牛人不屑于准备就去Google面试,结果被爆,与其说是能力问题,不如说是态度问题。原创 2015-06-15 10:46:20 · 3750 阅读 · 1 评论 -
LeetCode Contains Duplicate III
Given an array of integers, find out whether there are two distinct indices i and j in the array such that the difference between nums[i] and nums[j] is at most t and the difference between i and j is原创 2015-06-15 14:02:42 · 3816 阅读 · 0 评论 -
LeetCode Kth Smallest Element in a BST
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.Note: You may assume k is always valid, 1 ≤ k ≤ BST's total elements.Follow up:What if the BST is modifi原创 2015-07-19 15:11:29 · 3514 阅读 · 0 评论 -
LeetCode Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree.思路分析:这题主要考察递归,我们可以画一个二叉树的实例来具体分析。比如如果postorder 是84526731,inorder是84521637,首先我们可以知道postorder的最后一个数1必然是root,然后我们可以在inorder里面找到这原创 2015-01-28 12:24:49 · 2290 阅读 · 1 评论 -
LeetCode 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 binary tree and sum = 22原创 2015-01-28 13:46:02 · 1598 阅读 · 1 评论 -
LeetCode Path Sum II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.For example:Given the below binary tree and sum = 22, 5 / \原创 2015-01-28 14:44:23 · 2238 阅读 · 2 评论 -
POJ 2421 图的最小生成树 prim算法 模板代码的利用
这题调试了很久,开始自己试图按照自己的理解实现prim算法,但是总是出错。后来参考了网上的算法模板,总算把此题解决。算法模板可以用,但不可以滥用,最好是理解使用的细节,代码库不在多,好用是关键! 基本算法是:修好的路的边权值赋为0,再用prim求最小生成树输出权值。#include #define MAXN 200#define inf 10000typedef int elem_t;using namespace std;elem_t prim(int n,elem_原创 2010-11-22 20:45:00 · 2948 阅读 · 0 评论 -
POJ 3615 牛的最小最大起跳高度问题 Floyd算法的变形应用
此题求牛从起点到终点路径中最大权值最小的那条路径,将Floyd算法稍作修改即可,注意此题输入输出处理不当可能引起超时,一般scanf及printf更节省时间。#include #include #define MAX_VEX 305#define MAX_WEI 1000005using namespace std;int A[MAX_VEX][MAX_VEX];//用cin,cout会超时int main(){ int N,M,T,i,j,k,h; int原创 2010-11-24 22:48:00 · 1801 阅读 · 0 评论 -
POJ 1847 最短路问题 dijkstra算法的实现
首先自己练习了一下实现dijkstra算法,可以把dj算法与prim算法对比记忆,要理解pre数组、min数组、V标记数组的含义!//单源最短路径,dijkstra算法,邻接阵形式,复杂度O(n^2)//求出源s到所有点的最短路经,传入图的顶点数n,(有向)邻接矩阵mat//返回到各点最短距离min[]和路径pre[],pre[i]记录s到i路径上i的父结点,pre[s]=-1//可更改路权类型,但必须非负!//可以把dj算法与prim算法对比记忆,要理解pre数组、min数组、V标记数组的原创 2010-11-25 22:30:00 · 3677 阅读 · 0 评论 -
POJ1258 最小生成树prim算法
典型的prim算法这类题目可以稍作变形,比如POJ2421#include #include #define MAXN 102typedef long elem_t;using namespace std;elem_t prim(int n,elem_t mat[MAXN][MAXN]){ elem_t closeEdge[MAXN],sum=0,min;原创 2011-12-21 00:34:25 · 1741 阅读 · 0 评论 -
POJ 1251 最小生成树prim算法
还是最小生成树prim算法,套用的浙大模板#include #define MAXN 28#define inf 10000typedef int elem_t;using namespace std;elem_t prim(int n,elem_t mat[MAXN][MAXN],int* pre){ elem_t min[MAXN],ret=0; i原创 2011-12-21 00:35:54 · 1671 阅读 · 0 评论 -
二叉树的建立删除及三种遍历实现
二叉树的建立、删除及三种遍历的C++实现教材上的Tree类写法太累赘,不实用,将树节点直接写成结构体即可要理解结点的含义以及根结点的重要意义#include #include //用CPP文件编写,否则编译出错,需要加struct且delete不识别struct TreeNode { int val; TreeNode * left, * right;};TreeNode原创 2010-11-16 17:08:00 · 2461 阅读 · 4 评论 -
POJ 1789 求车类型字符串距离最小值 最小生成树 prim算法
题意:车的类型用字符串来描述,两个汽车类型的距离定义为其字符串中不同字符的个数,给定所有的车的类型,求车之间的派生关系,使得总的距离最短,派生关系质量分最大算法:转化为图论问题,结点对应不同的汽车类型,边的权值就是不同结点字符串距离值,求最小生成树即可(因为题目说明了每个汽车只有一种汽车派生出)Source CodeProblem: 1789 User: y原创 2011-12-26 01:35:49 · 2932 阅读 · 3 评论 -
POJ1062 昂贵的聘礼 单源最短路径变形 dijkstra算法
思路:以物品为结点,物品之间的优惠价格为边权值建图,酋长10000金币当做0号结点,题意就是求图中各结点到0号结点的最短路长度,再加上终点处物品的价值,恰好就是探险家经过这个物品买卖途径所需要付出的金钱。用dijkstra算法求出单源最短路径,从各个结点的最短路径中选出最短的那条就是答案。基本还是经典最短路问题,但做了一点小小变形主要是:1 有结点等级限制,需要枚举等级 2 把终点的物品价原创 2012-01-03 19:23:44 · 2897 阅读 · 0 评论 -
POJ2485 求最小生成树的最大边长度
此题关键要理解输出的定义For each test case, you should output a line contains an integer, which is the length of the longest road to be built such that all the villages are connected, and this value is minimu原创 2012-02-09 23:27:06 · 3006 阅读 · 0 评论 -
POJ 2499 求二叉树结点到根结点的路径长度 递归 二叉树
这题主要求二叉树结点到根结点的路径长度,基本的思路是 比较a与b,如果a大则当前结点是左孩子,a-b作为父结点的左数,父结点的右数与当前右数相等;如果b大则当前结点为右孩子,同理可以求父结点,直到父结点为(1,1)遍历结束。当用原始的递归算法会超时,需要考虑a=1或b=1的特殊情况,同时利用a与b的倍数关系加快遍历速度Source CodeProblem: 2499原创 2012-02-11 15:31:18 · 3242 阅读 · 0 评论 -
NYU AI作业习题-活动安排问题 BFS+DFS Iterative deepening depth-first search
题目链接 http://cs.nyu.edu/courses/spring12/CSCI-GA.2560-001/prog1.html题目大意:给定n个任务的时间、价值及先后序关系,求一个可行的任务子集,使得时间之和不大于deadline,价值之和不小于targetVaule,且不可出现逆序。算法思路:题目已经给出算法,转化为状态空间搜索问题(tree-structured state原创 2012-02-16 01:28:11 · 4390 阅读 · 0 评论 -
LeetCode Validate Binary Search Tree
LeetCode Validate Binary Search TreeGiven a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:The left subtree of a node contains only nodes with keys原创 2014-11-09 11:02:57 · 1894 阅读 · 0 评论 -
LeetCode 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 3 3 2 1 \ /原创 2015-08-31 12:42:57 · 2859 阅读 · 0 评论