
DFS
永远的EMT
每天时刻保持超越自我的意识
展开
-
【PAT】1018. Public Bike Management
考查点:DFS递归,Dijkstra求最短路径思路:这道题不能直接用Dijkstra来更新need和remain必须用DFS遍历所有最短路径才能更新,写Dijkstra记得判断vis[u]==false,路径用pre记录前驱节点,因为有多条最短路径,必须用Vector数组来保存,Dijkstra主要涉及的就是pre的更新,DFS每次递归到边界0处时候遍历temp路径计算need和remain,原创 2017-02-16 17:38:11 · 377 阅读 · 0 评论 -
判断有向图是否存在环
简介 前面讨论的很多文章里,都是针对无向图进行的分析。无向图的一个特性就是其中一旦两个节点a和b是相连的,这就意味着有路径从a到b,同时也有从b到a的。它具体对应的矩阵表达方式对应着一个对称矩阵。而这里重点是考察有向图。和无向图比起来,有向图更加多了一种出入度的概念。因为方向的有向性,很多以前在无向图里看起来比较简单的问题在这里会变得更加有意思。 有向图定义 一个常用的有向...转载 2018-09-04 22:36:43 · 21226 阅读 · 0 评论 -
【LeetCode】210. Course Schedule II
There are a total of n courses you have to take, labeled from 0 to n-1.Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair...原创 2018-09-08 23:27:37 · 168 阅读 · 0 评论 -
【LeetCode】207. Course Schedule
There are a total of n courses you have to take, labeled from 0 to n-1.Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair...原创 2018-09-07 13:09:55 · 131 阅读 · 0 评论 -
【LeetCode】827. Making A Large Island
In a 2D grid of 0s and 1s, we change at most one 0 to a 1.After, what is the size of the largest island? (An island is a 4-directionally connected group of 1s).Example 1:Input: [[1, 0], [0, 1]]...原创 2018-09-30 22:28:23 · 208 阅读 · 0 评论 -
【LeetCode】200. Number of Islands
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume...原创 2018-09-30 22:31:27 · 162 阅读 · 0 评论 -
【LeetCode】113. 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.Note: A leaf is a node with no children.Example:Given the below binary tree and sum = 22,...原创 2018-10-06 23:14:33 · 330 阅读 · 0 评论 -
【LeetCode】130. Surrounded Regions
Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'.A region is captured by flipping all 'O's into 'X's in that surrounded region.Example:X X X XX O O...原创 2018-10-15 22:11:55 · 137 阅读 · 0 评论 -
【LeetCode】131. Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome.Return all possible palindrome partitioning of s.Example:Input: "aab"Output:[ ["aa","b"], ["a","a"...原创 2018-10-15 22:32:43 · 151 阅读 · 0 评论 -
【LeetCode】133. Clone Graph
Given the head of a graph, return a deep copy (clone) of the graph. Each node in the graph contains a label (int) and a list (List[UndirectedGraphNode]) of its neighbors. There is an edge between the ...原创 2018-10-15 22:35:42 · 152 阅读 · 0 评论 -
【LeetCode】129. Sum Root to Leaf Numbers
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.An example is the root-to-leaf path 1->2->3 which represents the number 123.Find the tota...原创 2018-10-15 23:13:15 · 277 阅读 · 0 评论 -
【LeetCode】126. Word Ladder II
Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformation sequence(s) from beginWord to endWord, such that:Only one letter can be changed at a time Eac...原创 2018-10-17 01:07:30 · 273 阅读 · 0 评论 -
【LeetCode】Unique Binary Search Trees II
Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ... n.Example:Input: 3Output:[ [1,null,3,2], [3,2,null,1], [3,1,null,null,2], [2,1,3...原创 2018-10-22 14:23:38 · 311 阅读 · 0 评论 -
【LeetCode】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 3 But the follo...原创 2018-10-20 22:46:22 · 196 阅读 · 0 评论 -
【LeetCode】Validate Binary Search Tree
Given 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 less than the node's key. The ...原创 2018-10-21 22:49:10 · 167 阅读 · 0 评论 -
【LeetCode】Word Search
Given a 2D board and a word, find if the word exists in the grid.The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically n...原创 2018-11-07 01:26:43 · 222 阅读 · 0 评论 -
【Leetcode】785. Is Graph Bipartite?
Given an undirected graph, return true if and only if it is bipartite.Recall that a graph is bipartite if we can split it's set of nodes into two independent subsets A and B such that every edge in ...原创 2018-08-28 14:58:19 · 332 阅读 · 0 评论 -
【Leetcode】802. Find Eventual Safe States
In a directed graph, we start at some node and every turn, walk along a directed edge of the graph. If we reach a node that is terminal (that is, it has no outgoing directed edges), we stop.Now, sa...原创 2018-08-27 13:06:43 · 184 阅读 · 0 评论 -
【LeetCode】399. Evaluate Division
Equations are given in the format A / B = k, where A and B are variables represented as strings, and k is a real number (floating point number). Given some queries, return the answers. If the answer d...原创 2018-07-29 23:14:59 · 263 阅读 · 0 评论 -
【PAT】1103. Integer Factorization
考查点:深搜,转化为一类问题:枚举从N个整数中选择K个数的所有方案。这里只是稍微变形了一下,首先要做预处理,构造出可以选择的N个数,可以放到一个Vector里面,这类题的模版是:void DFS(int index,int curK,int sum,int facSum){ if(sum==n&&curK==k){ if(facSum>mSum){原创 2017-02-11 19:32:25 · 285 阅读 · 0 评论 -
【PAT】1090. Highest Price in Supply Chain
考查点:树的遍历,计算树的最大层数思路:利用Vector存放树的叶节点,DFS深搜,记录最大层数#define LOCAL#include #include #include #include #include #include #include #include #include #include #include #define FOR(i, x, y) for原创 2017-02-12 15:18:53 · 231 阅读 · 0 评论 -
【PAT】1094. The Largest Generation
考查点:BFS或DFS,求树的最大节点层思路:BFS代码长点,DFS较短,BFS只需设置个变量last来记录每一层的最后一个节点;DFS只需两个参数,一个节点一个层数,每次调用都要更新当前层的节点数,可以用哈希数组保存层数的节点值提交情况:一开始输入时候%d写错成%k调了半天,之后因为找根节点时候没把0排除,第一次提交没考虑只有一个根节点的情况之后acBFS的代码:#define原创 2017-02-12 17:25:59 · 275 阅读 · 0 评论 -
【PAT】1079. Total Sales of Supply Chain
考查点:DFS或BFS思路:直接用DFS即可,代码简洁,没有难度#define LOCAL#include #include #include #include #include #include #include #include #include #include #include #define FOR(i, x, y) for(int i = x; i <原创 2017-02-12 18:08:12 · 310 阅读 · 0 评论 -
【PAT】1106. Lowest Price in Supply Chain
#define LOCAL#include #include #include #include #include #include #include #include #include #include #include #define FOR(i, x, y) for(int i = x; i < y; i++)#define rFOR(i, x, y) for(i原创 2017-02-12 19:14:24 · 219 阅读 · 0 评论 -
【PAT】1004. Counting Leaves
考查点:BFS或DFS思路:本题为求各层叶节点个数,深搜和广搜都可以,深搜写起来更方便DFS版本:#define LOCAL#include #include #include #include #include #include #include #include #include #include #include #define FOR(i, x, y)原创 2017-02-12 20:52:21 · 246 阅读 · 0 评论 -
【PAT】1053. Path of Equal Weight
考查点:DFS思路:本题要求的是多条符合条件的路径且要字典序逆序输出,首先要输出符合条件的路径可以用DFS,关键技巧是在DFS增加一个参数numnode表示路径中的节点数,其次多个路径要逆序输出只须输入时预处理即可,即把子节点按权值从大到小排序#define LOCAL#include #include #include #include #include #include原创 2017-02-12 22:58:34 · 315 阅读 · 0 评论 -
图的遍历DFS
邻接矩阵版:int n,G[MAXN][MAXN];bool vis[MAXN];void DFS(int u,int depth){ vis[u]=true; FOR(v,0,n) { if(vis[v]==false&&G[u][v]!=oo){ DFS(v,depth+1); } }}voi原创 2017-02-14 11:16:47 · 547 阅读 · 0 评论 -
【PAT】1013. Battle Over Cities
考查点:DFS遍历图,计算连通分量思路:本质上就是计算连通分量,DFS遇到删除节点时要退出提交情况:注意这里是1到n下标要对应好#define LOCAL#include #include #include #include #include #include #include #include #include #include #include #defin原创 2017-02-14 14:11:40 · 325 阅读 · 0 评论 -
【PAT】1021. Deepest Root
考查点:并查集求连通分量,DFS或BFS思路:本题数据很弱,第一次直接用DFS就可以过,主要犯了啃爹的错误,在写循环遍历容器时for(int i=0;i暴力过的代码:#include #include #include #include #include #include #include #include #include #include #include #原创 2017-02-15 00:24:42 · 264 阅读 · 0 评论 -
【PAT】1110. Complete Binary Tree
考查点:二叉树,完全二叉树,DFS思路及提交情况:第一次输入接受字符时没考虑getchar接受换行,第二次完全二叉树下标没有从1开始,最后ans记录二叉树节点的数组n-1没有修改为n,完全改好后一直段错误实际上是程序错误,就是接收数据时出错了,只接受字符,所以节点是二位数时输入就会溢出出现段错误,以后段错误要记得可能是输入有问题,还有不要理所当然从样例看出一定是接收字符,有可能是两位数。#原创 2017-02-23 23:53:17 · 351 阅读 · 0 评论 -
【PAT】1115. Counting Nodes in a BST
考查点:BST插入,DFS思路及提交情况:水题,计算最后两层的节点数,只需维护一个层数数组,记录该层的节点数,用DFS是最简洁的,这里开始遇到bug,首先新建节点要么new要么NULL,之后记得return#define LOCAL#include #include #include #include #include #include #include #include原创 2017-02-24 17:39:20 · 341 阅读 · 0 评论 -
【PAT】1119. Pre- and Post-order Traversals
考查点:二叉树的遍历思路及提交情况:第一次各种格式错误和错误,格式错误居然是最后要换行。。第一次是判断是否唯一时i——后序遍历中第一个等于先序序列左子树根节点的点位置应该在后续序列的左边界前因为本来就是i==postR-1的,如果越界了应该排除,此题思路就是先序遍历的思路重建二叉树,关键在于维护各个序列的边界值#define LOCAL#include #include #inclu原创 2017-02-25 00:03:56 · 430 阅读 · 0 评论 -
【PAT】1034. Head of a Gang
考查点:DFS或并查集思路:首先本题的节点是用字符串标识的必须用map转化为int,这里只能转化为1到n的连续数,所以要预处理的时候用计数的方法转化;计算最大权值点和图的节点数可以用并查集,注意并查集这里要在初始化的时候保证所有点都指向head即最大权值点,每次修正根节点记得把根节点指向自己,还需要用一个sum数组更新每个连通分量的总权值,一个数组记录每个点的权值,用DFS的话参数需要增加he原创 2017-02-15 20:34:28 · 364 阅读 · 0 评论 -
【LeetCode】100. Same Tree
题解:二叉树的遍历,dfsbool isSameTree(TreeNode* p, TreeNode* q) { if(p==NULL||q==NULL)return p==q; return p->val==q->val&&isSameTree(p->left,q->left)&&isSameTree(p->right,q->right);}原创 2017-08-24 15:28:22 · 277 阅读 · 0 评论 -
【LeetCode】212. Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board.Each word must be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horiz...原创 2018-11-10 19:00:07 · 167 阅读 · 0 评论