1//exactly the same as BFS except used a stack to mimic the recursion, technically any BFS can be written2//in DFS by using a stack3publicNode cloneGraphDFSUsingStack(Node node) {4if(node ==null) {5returnnode;6}
popleft() sorted_order += vertex # Find children children = graph[vertex] for child in children: in_degree[child] -= 1 if in_degree[child] == 0: sources.append(child) # Should have no cycle problem (it's alien language) return str(sorted_order) solution = Solution() print(solution...
class Solution { public: vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) { if(n==1){ return vector<int>{0}; }else if(n==2){ return edges[0]; } Graph graph(n,edges); vector<int> result; int min_dist = n; for(int i=0;i<n;i++){ int dist = graph....
222 Count Complete Tree Nodes // #222 统计完全二叉树节点个数 描述:如题。 //#222Description: Count Complete Tree Nodes | LeetCode OJ 解法1:可以二分。 // Solution 1: Binary search. 代码1 //Code 1 223 Rectangle Area // #223 矩形面积 描述:给定两个横平竖直的矩形,求总覆盖面积。 //#223...
You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times) with the following restrictions: You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again). After you sell your stock, ...
222 count-complete-tree-nodes Medium 223 rectangle-area Medium 224 basic-calculator Hard 225 implement-stack-using-queues 📝 Easy 226 invert-binary-tree 📝 Easy 227 basic-calculator-ii Python Medium 228 summary-ranges 📝 Medium 229 majority-element-ii Medium 230 kth-smallest-element-in-a-bs...
0919 Complete Binary Tree Inserter 64.9% Medium 0920 Number of Music Playlists Go 50.6% Hard 0921 Minimum Add to Make Parentheses Valid Go 76.4% Medium 0922 Sort Array By Parity II Go 70.7% Easy 0923 3Sum With Multiplicity Go 45.4% Medium 0924 Minimize Malware Spread Go 42.1% Hard ...
dfs判断图是否有环:Detect Cycle In Directed/Undirected Graph 判断是否是连通图很容易,在dfs一个node后,只要看看是不是所有节点都visit过了即可。 也可以用图论的知识,对于Tree,|E| = |V|-1 时间复杂度 O(V+E) classSolution {public: vector<list<int>>adj;boolvalidTree(intn, vector<vector<int>>&...
0919 Complete Binary Tree Inserter 57.3% Medium 0920 Number of Music Playlists Go 46.5% Hard 0921 Minimum Add to Make Parentheses Valid Go 73.6% Medium 0922 Sort Array By Parity II Go 69.2% Easy 0923 3Sum With Multiplicity Go 35.7% Medium 0924 Minimize Malware Spread Go 42.0% Hard ...
LeetCode 刷题随手记 - 第一部分 前 256 题(非会员),仅算法题,的吐槽 https://leetcode.com/problemset/algorithms/...