在写BFS的时候,经常会用到queue来标记需要验证的点(即由上一层而关联的各个点,就是所谓的灰色的点。黑色的点是queue最上面的那个点,就是正在被process会被poll出来那个)。然后辅助一个hash来记录是否已经visit过这个node。

public class Solution {/*** @param n an integer* @param edges a list of undirected edges* @return true if it's a valid tree, or false*/public boolean validTree(int n, int[][] edges) {// Write your code here//To find cycle and not connect nodeint nEdges = edges.length;//If total number of the node is n, for the tree(undirection graph), //the number of the edges should be n - 1. //If there are more than n - 1 edges, there are cycles.//If there are less than n - 1 edges, some nodes are not connected.if ( n != (nEdges + 1)) {return false;}//After the nEdge validation, we need to consider the on has cycles, //but also contains un-fully-connected(which only have one or zero edge) nodes.//So we can create a hashset to remeber all the nodes that is connected.//And a Queue is also created to put the node that got connected.//If there are cycles and breaks, //in the end the node which is not fully connected will not be able to add to the queue//In the end, we just need to compare the size of hashset with n. HashMap<Integer, HashSet<Integer>> graph = initalizeGraph(n,edges); HashSet<Integer> cnntNodes = new HashSet<Integer>();Queue<Integer> queue = new LinkedList<Integer>();queue.offer(0);cnntNodes.add(0);while (!queue.isEmpty()) {int curNode = queue.poll();HashSet<Integer> cnntToThisNode = graph.get(curNode);for (Integer i : cnntToThisNode) {if (cnntNodes.contains(i)) {continue;}cnntNodes.add(i);queue.offer(i);}}return (cnntNodes.size() == n);}private HashMap<Integer, HashSet<Integer>> initalizeGraph(int n, int[][] edges) {HashMap<Integer, HashSet<Integer>> graph = new HashMap<>();for (int i = 0; i < n; i++) {graph.put(i, new HashSet<Integer>());}for (int i = 0; i < edges.length; i++) {int u = edges[i][0];int v = edges[i][1];graph.get(u).add(v);graph.get(v).add(u);}return graph;}}

转载于:https://www.cnblogs.com/codingEskimo/p/6565105.html

Graph Valid Tree相关推荐

  1. 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 ...

  2. 【Lintcode】444. Graph Valid Tree II

    题目地址: https://www.lintcode.com/problem/444/ 要求设计一个数据结构,可以做如下两个操作: 1.void addEdge(int a, int b)在aaa与b ...

  3. 【LeetCode】图论 graph(共20题)

    [133]Clone Graph (2019年3月9日,复习) 给定一个图,返回它的深拷贝. 题解:dfs 或者 bfs 都可以 1 /* 2 // Definition for a Node. 3 ...

  4. 继续过中等难度.0309

      .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Mediu ...

  5. 【LeetCode】深搜DFS(共85题)

    [98]Validate Binary Search Tree [99]Recover Binary Search Tree [100]Same Tree [101]Symmetric Tree [1 ...

  6. [leetcode] 题型整理之图论

    图论的常见题目有两类,一类是求两点间最短距离,另一类是拓扑排序,两种写起来都很烦. 求最短路径: 127. Word Ladder Given two words (beginWord and end ...

  7. Leetcode重点250题

    LeetCode重点250题 这个重点题目是把LeetCode前400题进行精简.精简方法如下: 删除不常考,面试低频出现题目 删除重复代码题目(例:链表反转206题,代码在234题出现过) 删除过于 ...

  8. Leetcode总结之Union Find

    package UnionFind;import java.util.ArrayList; import java.util.LinkedList; import java.util.List;pub ...

  9. leetcode刷题规划

    LeetCode精华题目列表[刷题规划系列] – TuringPlanet 目录 算法题到底在考察什么? 题目列表 Array String Linked List Queue Stack Advan ...

最新文章

  1. div 相同属性提取
  2. Java Collections.sort方法对list集合排序
  3. python笔记:断言assert
  4. DL之ResNet:ResNet算法的简介(论文介绍)、架构详解、案例应用等配图集合之详细攻略
  5. oracle之报错:ORA-00054: 资源正忙,要求指定 NOWAIT
  6. java代码如何避免死锁,Java可重入锁如何避免死锁
  7. 对话系统(四)- RNN
  8. linux进入命令是什么,linux进入目录的命令是什么
  9. 原生JDBC和工具类的基本实现
  10. Facebook 中国程序员之死
  11. STL源码剖析学习二:空间配置器(allocator)
  12. leetcode - Minimum Depth of Binary Tree
  13. C# 调用Dll中非托管C++代码时,函数参数的类型对照
  14. liunx trac 插件使用之GanttCalendarPlugin
  15. android hfp分析,Android HFP-转
  16. 论文理解记录:The Lottery Ticket Hypothesis
  17. java编程个人总结_java个人总结
  18. 点击按钮打开新页面(携带参数)
  19. 低成本OpenPNP驱动0816飞达
  20. 两分钟了解数据封装和解封

热门文章

  1. Linux、Ubuntu、CentOS安装和配置zsh
  2. linux zabbix使用教程,《Zabbix安装部署》-Centos7
  3. linux下 java 文本_Java中如何将输入的信息写入文本中
  4. ol+天地图+geoserver_GeoServer的WMS服务加载到天地图
  5. java vim ide_把VIM配置成IDE开发环境 | 学步园
  6. 4g内存只有1.6g可用_linux服务器内存异常,究竟在哪消耗了2.5G?
  7. java checkbox数组_Java中的复选框数组
  8. 在JS中使用trim 方法
  9. sun.misc.unsafe类的使用
  10. 【BZOJ1433】【codevs2347】假期的宿舍,最大流