Leetcode 797 所有可能的路徑 C

2021-10-08 21:32:19 字數 605 閱讀 8429

給乙個有 n 個結點的有向無環圖,找到所有從 0 到 n-1 的路徑並輸出(不要求按順序)

輸入: [[1,2], [3], [3], ] 

輸出: [[0,1,3],[0,2,3]]

解釋: 圖是這樣的:

0--->1

| |

v v

2--->3

這有兩條路: 0 -> 1 -> 3 和 0 -> 2 -> 3.

dfs深搜回溯,從節點0開始遍歷,終點為n-1。詳細過程見**

vectorint>> ans;

vector<

int> visit;

//標記已訪問節點

void

dfs(vector<

int>

& rote,

int target,vectorint>>

& graph)

else}}

} vectorint>>

allpathssourcetarget

(vectorint>>

& graph)

797 所有可能的路徑

一 dfs回溯 class solution object 方法一 dfs def allpathssourcetarget self,graph type graph list list int rtype list list int if not graph 0 return res n len...

133 所有可能的路徑

題目描述 給乙個有 n 個結點的有向無環圖,找到所有從 0 到 n 1 的路徑並輸出 不要求按順序 結點的數量會在範圍 2,15 內。你可以把路徑以任意順序輸出,但在路徑內的結點的順序必須保證。使用回溯和遞迴進行 class solution public static void dfspath l...

Leetcode894 所有可能的滿二叉樹

滿二叉樹是一類二叉樹,其中每個結點恰好有 0 或 2 個子結點。返回包含 n 個結點的所有可能滿二叉樹的列表。答案的每個元素都是乙個可能樹的根結點。答案中每個樹的每個結點都必須有 node.val 0。你可以按任何順序返回樹的最終列表。示例 1 先判斷特殊情況 由於滿二叉樹中每個結點都有0或者2個結...