题库chevron_right图·DFS·traversal

图·DFS·traversal 模式

43 道题目

模式页适合用来建立可复用解题框架。先识别题目特征,再按固定流程解释状态定义、转移和边界。

识别信号

  • Do you understand the importance of using a hash table to store visited nodes during graph traversal?
  • Can you describe how to handle cycles when cloning a graph using DFS?
  • Candidate identifies graph representation with adjacency lists.

解题流程

  1. 1. 明确窗口/状态定义
  2. 2. 更新状态并维护约束
  3. 3. 用边界样例验证

常见失分点

  • Not using a hash table to track cloned nodes, leading to cycles and duplicate clones.
  • Failing to sort adjacency lists leading to incorrect lexical order.
  • Forgetting to add the inverse edge 1/value, which breaks paths in DFS.

推荐题单梯度

#题目难度
133

克隆图

Clone Graph involves cloning a graph using DFS, focusing on graph traversal and neighbor management using hash tables.

中等
332

重新安排行程

Reconstruct Itinerary requires building a valid travel route using all tickets once, starting from JFK with lexical orde…

困难
399

除法求值

Compute the results of division queries from given equations using graph traversal and depth-first search efficiently.

中等
547

省份数量

Solve Number of Provinces by scanning the adjacency matrix and launching DFS once per unvisited city component.

中等
684

冗余连接

Identify and remove the redundant edge that causes a cycle in a graph starting as a tree.

中等
685

冗余连接 II

Find and remove the redundant connection in a directed graph that was originally a rooted tree.

困难
743

网络延迟时间

Find the minimum time for a signal to travel to all nodes in a directed graph or determine if it's impossible.

中等
753

破解保险箱

The Cracking the Safe problem involves finding the shortest password sequence to unlock a safe using graph traversal and…

困难
765

情侣牵手

This problem requires arranging couples sitting apart in a row with the minimum number of swaps using graph traversal an…

困难
785

判断二分图

Determine whether an undirected graph can be split into two independent sets using DFS, BFS, or Union Find patterns.

中等
787

K 站中转内最便宜的航班

Find the cheapest flight from a source to a destination with at most K stops using graph traversal techniques efficientl…

中等
797

所有可能的路径

Find all paths in a directed acyclic graph (DAG) from source to target using depth-first search and backtracking.

中等
834

树中距离之和

The problem asks to compute the sum of distances between each node and all others in a tree structure using depth-first …

困难
841

钥匙和房间

Determine if all rooms can be visited given keys distributed across a set of interconnected locked rooms using graph tra…

中等
886

可能的二分法

Determine if a group of n people with mutual dislikes can be split into two non-conflicting groups using graph traversal…

中等
947

移除最多的同行或同列石头

Maximize the number of stones removed from a 2D plane using graph traversal and DFS.

中等
1042

不邻接植花

In this problem, you are tasked with planting flowers in gardens with specific constraints based on graph traversal prin…

中等
1192

查找集群内的关键连接

Find critical connections in a network of servers, ensuring efficient traversal using depth-first search and Tarjan's al…

困难
1319

连通网络的操作次数

Determine the minimum number of operations required to connect all computers in a network with a limited number of cable…

中等
1361

验证二叉树

Validate Binary Tree Nodes problem requires checking if a set of nodes forms a valid binary tree using graph traversal.

中等
1377

T 秒后青蛙的位置

The problem asks for the probability that a frog reaches a target vertex after t seconds in a tree graph.

困难
1466

重新规划路线

Reorder Routes to Make All Paths Lead to the City Zero involves reversing the direction of roads in a tree to ensure all…

中等
1971

寻找图中是否存在路径

Determine if a valid path exists between two vertices in a bi-directional graph using traversal techniques.

简单
2092

找出知晓秘密的所有专家

Find all people who receive a secret through meetings using graph traversal with depth-first search efficiently and corr…

困难
2097

合法重新排列数对

Given pairs of numbers, find a valid arrangement where each pair follows a specific condition.

困难
2101

引爆最多的炸弹

Determine the maximum number of bombs that can be detonated by leveraging chain reactions using graph traversal and DFS …

中等
2316

统计无向图中无法互相到达点对数

Calculate the total number of node pairs in an undirected graph that cannot reach each other using DFS, BFS, or Union Fi…

中等
2359

找到离给定两个节点最近的节点

Find the node that minimizes the maximum distance to two given nodes in a directed graph with one outgoing edge per node…

中等
2467

树上最大得分和路径

Solve the 'Most Profitable Path in a Tree' problem using graph traversal and depth-first search techniques to maximize A…

中等
2477

到达首都的最少油耗

Calculate the minimum fuel needed for all city representatives to reach the capital using DFS on a tree graph efficientl…

中等
2492

两个城市间路径的最小分数

Find the minimum distance in a path connecting two cities using graph traversal strategies efficiently.

中等
2493

将节点分成尽可能多的组

Determine the maximum number of groups nodes can form in a graph using depth-first traversal without violating edge conn…

困难
2646

最小化旅行的价格总和

Calculate the minimum total cost of multiple trips on a tree by selectively halving node prices using DFS frequency coun…

困难
2685

统计完全连通分量的数量

Determine the number of complete connected components in an undirected graph using efficient traversal techniques and gr…

中等
2858

可以到达每一个节点的最少边反转次数

This problem requires solving a graph traversal with edge reversals to ensure every node is reachable in a tree-like str…

困难
3123

最短路径中的边

Use two Dijkstra runs to mark exactly which edges can appear on at least one shortest path from 0 to n - 1.

困难
3203

合并两棵树后的最小直径

Calculate the minimum diameter after merging two trees by strategically connecting nodes to minimize the longest path in…

困难
3241

标记所有节点需要的时间

Calculate the time taken to mark all nodes in a tree, starting from any node with time t=0.

困难
3310

移除可疑的方法

Remove suspicious methods in a project that are invoked directly or indirectly from a buggy method using graph traversal…

中等
3387

两天自由外汇交易后的最大货币数

Compute the maximum currency amount after two days using graph traversal and depth-first search for optimal conversions.

中等
3419

图的最大边权的最小值

Minimize the maximum edge weight in a graph after removing certain edges while ensuring node reachability.

中等
3528

单位转换 I

Solve the unit conversion problem by using graph traversal to compute equivalent unit amounts.

中等
3547

图中边值的最大和

Maximize the sum of edge values in a connected graph by assigning unique node values and optimizing edge products.

困难

关联题型

LeetCode 图·DFS·traversal模式题解:43题训练路线