Learn Algorithms from LeetCode-121 (0802. Find Eventual Safe States)

Categories: Graph/DFS, Level: Medium

Chih-Yu Lin
5 min readMay 1, 2024


Introduction: (Basic + Advanced + Interview Edition)
Learning Algorithms from LeetCode + Interview Success Guide
(Regular price: 3591 NTD, Discounted price until 2024/05/16 23:59)

(Regular price: 3990 NTD, Valid for a long term)

Please help me click on the “SHOW EMBED” below and give it 5 likes~ If you like it, you can also give me a round of applause~
(Liking doesn’t cost anything, thank you for supporting my writing~)

There is a directed graph of n nodes with each node labeled from 0 to n - 1. The graph is represented by a 0-indexed 2D integer array graph where graph[i] is an integer array of nodes adjacent to node i, meaning there is an edge from node i to each node in graph[i].

A node is a terminal node if there are no outgoing edges. A node is a safe node if every possible path starting from that node leads to a terminal node (or another…



Chih-Yu Lin

LeetCode、Python、Java、Android;第11屆iT邦幫忙鐵人賽Software Development組優選(從LeetCode學演算法);HiSKIO特約講師;課程優惠: https://bit.ly/lc2022all ;合作請洽: learnwithdesolve@gmail.com