Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), write a function to check whether these edges make up a valid tree.

For example:

Given n = 5 and edges = [[0, 1], [0, 2], [0, 3], [1, 4]], return true.

Given n = 5 and edges = [[0, 1], [1, 2], [2, 3], [1, 3], [1, 4]], return false.

Hint:

Given n = 5 and edges = [[0, 1], [1, 2], [3, 4]], what should your return? Is this case a valid tree? Show More Hint Note: you can assume that no duplicate edges will appear in edges. Since all edges are undirected, [0, 1] is the same as [1, 0] and thus will not appear together in edges.

 public boolean validTree(int n, int[][] edges) {
HashMap<Integer, ArrayList<Integer>> map = new HashMap<Integer, ArrayList<Integer>>();
for(int i=; i<n; i++){
ArrayList<Integer> list = new ArrayList<Integer>();
map.put(i, list);
} for(int[] edge: edges){
map.get(edge[]).add(edge[]);
map.get(edge[]).add(edge[]);
} boolean[] visited = new boolean[n]; LinkedList<Integer> queue = new LinkedList<Integer>();
queue.offer();
while(!queue.isEmpty()){
int top = queue.poll();
if(visited[top])
return false; visited[top]=true; for(int i: map.get(top)){
if(!visited[i])
queue.offer(i);
}
} for(boolean b: visited){
if(!b)
return false;
} return true;
}
 public class Solution {
public boolean validTree(int n, int[][] edges) {
// initialize n isolated islands
int[] nums = new int[n];
Arrays.fill(nums, -); // perform union find
for (int i = ; i < edges.length; i++) {
int x = find(nums, edges[i][]);
int y = find(nums, edges[i][]); // if two vertices happen to be in the same set
// then there's a cycle
if (x == y) return false;
// union
nums[x] = y;
}
return edges.length == n - ;
} int find(int nums[], int i) {
if (nums[i] == -) return i;
return find(nums, nums[i]);
}
}

Graph Valid Tree的更多相关文章

  1. [Locked] Graph Valid Tree

    Graph Valid Tree Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is ...

  2. [LeetCode] Graph Valid Tree 图验证树

    Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...

  3. Leetcode: Graph Valid Tree && Summary: Detect cycle in undirected graph

    Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...

  4. [LeetCode#261] Graph Valid Tree

    Problem: Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair o ...

  5. [Swift]LeetCode261.图验证树 $ Graph Valid Tree

    Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...

  6. [LeetCode] 261. Graph Valid Tree 图是否是树

    Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...

  7. LeetCode Graph Valid Tree

    原题链接在这里:https://leetcode.com/problems/graph-valid-tree/ 题目: Given n nodes labeled from 0 to n - 1 an ...

  8. 261. Graph Valid Tree

    题目: Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nod ...

  9. [LeetCode] 261. Graph Valid Tree _ Medium tag: BFS

    Given n nodes labeled from 0 to n-1 and a list of undirected edges (each edge is a pair of nodes), w ...

随机推荐

  1. LINQ的左连接、右连接、内连接

    .左连接: var LeftJoin = from emp in ListOfEmployees join dept in ListOfDepartment on emp.DeptID equals ...

  2. phpstorm webstorm安装主题 sublime样 还有都可以用的注册码

    注册码 webstorm phpstorm 基本所有版本通吃  webstrom9.0.3 通过 phpstorm 8.0.1 User Name: EMBRACE License Key: ==== ...

  3. Effective Objective-C 2.0 — 第三条:多用字面量语法,少用与之等价的方法

    第三条:多用字面量语法,少用与之等价的方法 几个类:NSString  NSNumber  NSArray  NSDictionary 字面量语法是一种语法糖(syntactic sugar) NSS ...

  4. [Js/Jquery]Jquery tagsinput在h5邮件客户端中应用

    摘要 最近一直在折腾邮件的h5应用,为了保证在pc,ios,android端都可以使用,所以使用H5页面的方式嵌入app的webview中. 页面 UI大概是这样的 Jquery tagsinput下 ...

  5. Linux运维初级教程(一)Shell脚本

    序,掌握shell脚本是linux运维工程师的最基础技能. 一.脚本语言 与高级语言不通,脚本语言运行时需要调用相应的解释器来翻译脚本中的内容. 常见的脚本语言有Shell脚本.Python脚本.ph ...

  6. AngularJS 使用$sce控制代码安全检查

    由于浏览器都有同源加载策略,不能加载不同域下的文件.也不能使用不合要求的协议比如file进行访问. 在angularJs中为了避免安全漏洞,一些ng-src或者ng-include都会进行安全校验,因 ...

  7. Spring注入方式

  8. List对象排序通用方法

    import java.util.Collections; import java.util.Comparator; import java.util.List; import java.lang.r ...

  9. 用SQLite查看编辑android导出的微信聊天记录

    上一篇我们已经能够完成文字版微信聊天记录导出android了,也即复制或剪切MicroMsg.db文件到电脑,以.db格式结尾的文件是数据库文件(database document),需要安装相关数据 ...

  10. data_quick 进度

    20131118  加入xml对L8卫星波段控制: 纠正波段顺序 QVProc20131125.7z: 完成L8的增强 以后的程序会添加ZY3号解析模块 QVProc20131126.7z: 新增 粗 ...