[抄题]:

给出 n 个节点,标号分别从 0 到 n - 1 并且给出一个 无向边的列表 (给出每条边的两个顶点), 写一个函数去判断这张`无向`图是否是一棵树。

给出n = 5 并且 edges = [[0, 1], [0, 2], [0, 3], [1, 4]], 返回 true.

给出n = 5 并且 edges = [[0, 1], [1, 2], [2, 3], [1, 3], [1, 4]], 返回 false.

[暴力解法]:

时间分析:

空间分析:

[思维问题]:

[一句话思路]:

树中不能有环,两点+老大哥三角成环。遍历所有边并且缩点,一旦出现公共祖先就退出。

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 树的基本性质是: 边= 点数 - 1,若不符合则退出

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

树中不能有环。

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

两点+老大哥三角成环,union find可以找老大哥。

[关键模板化代码]:

class UnionFind {
HashMap<Integer, Integer> father = new HashMap<>(); UnionFind(int n) {
for (int i = 0; i < n; i++) {
father.put(i,i);
}
} int compressed_find(int x) {
//find ultimate parent
int parent = x;
while (parent != father.get(parent)) {
parent = father.get(parent);
}
//change 2 ultimate parent
int temp = -1;
int fa = x;
while (fa != father.get(fa)) {
temp = father.get(fa);
father.put(fa,parent);
fa = temp;
}
return parent;
} void union (int x, int y) {
int fa_x = compressed_find(x);
int fa_y = compressed_find(y);
if (fa_x != fa_y) {
father.put(fa_x,fa_y);
}
}
}

并查集class

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

public class Solution {
/*
* @param n: An integer
* @param edges: a list of undirected edges
* @return: true if it's a valid tree, or false
*/
//class
class UnionFind {
HashMap<Integer, Integer> father = new HashMap<>(); UnionFind(int n) {
for (int i = 0; i < n; i++) {
father.put(i,i);
}
} int compressed_find(int x) {
//find ultimate parent
int parent = x;
while (parent != father.get(parent)) {
parent = father.get(parent);
}
//change 2 ultimate parent
int temp = -1;
int fa = x;
while (fa != father.get(fa)) {
temp = father.get(fa);
father.put(fa,parent);
fa = temp;
}
return parent;
} void union (int x, int y) {
int fa_x = compressed_find(x);
int fa_y = compressed_find(y);
if (fa_x != fa_y) {
father.put(fa_x,fa_y);
}
}
} public boolean validTree(int n, int[][] edges) {
//corner case is special
if (edges.length != n - 1) {
return false;
}
UnionFind uf = new UnionFind(n);
for (int i = 0; i < edges.length; i++) {
if (uf.compressed_find(edges[i][0]) ==
uf.compressed_find(edges[i][1])) {
return false;
}
uf.union(edges[i][0], edges[i][1]);
}
return true;
}
}

解法2:

323进化而来

添加每一条边 root1 == root0代表有环,不行

count > 1代表分块,不行

class Solution {
public boolean validTree(int n, int[][] edges) {
//use union find
//ini
int count = n;
int[] roots = new int[n]; //cc
if (n == 0 || edges == null) return true; //initialization the roots as themselves
for (int i = 0; i < n; i++)
roots[i] = i; //add every edge
for (int[] edge : edges) {
int root0 = find(edge[0], roots);
int root1 = find(edge[1], roots); if (root0 == root1) return false; //connect but is not merge
roots[root0] = root1;
count--;
} //return
return count == 1;
} public int find(int id, int[] roots) {
while (id != roots[id])
id = roots[roots[id]];
return id;
}
}

图是否是树 · Graph Valid Tree的更多相关文章

  1. [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), ...

  2. [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 ...

  3. [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), ...

  4. [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), ...

  5. 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), ...

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

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

  8. Graph Valid Tree -- LeetCode

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

  9. 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), ...

随机推荐

  1. 在webView 中使用JS 调用 Android / IOS的函数 Function

    最近做一个项目,混合了NativeCode 和 HTML,为了便于JS 调用App的一些方法,统一封装一个Js方法,记录如下 Android 端首先要再WebView中允许JS的调用 WebView ...

  2. Linux之screen命令详解

    一.nohup 工作中经常会遇到这样的需求,通过SecureCRT或其它工具远程到服务器执行某个任务,而这个任务耗时又比较长,你又不得不等待它执行完毕,但是如果此间如果关掉窗口或断开连接又会导致任务被 ...

  3. iframe相关知识

    iframe 不带边框的iframe因为能和网页无缝的结合从而不刷新页面的情况下更新页面的部分数据成为可能,可是 iframe的大小却不像层那样可以“伸缩自如”,所以带来了使用上的麻烦.一般通过百分比 ...

  4. 集合(List、Set、Map)

    List,Set是继承自Collection接口,Map不是 public interface List<E> extends Collection<E> { public i ...

  5. USB相关的sysfs文件

    主要来自driver/usb/core/sysfs.c: 1.bConfigurationValue RW,W时调用了usb_set_configuration()实时设置配置.根据USB规范(例如第 ...

  6. StreamSets 部署 Pipelines 到 SDC Edge

    可以使用如下方法: 下载edge 运行包并包含pipeline定义文件. 直接发布到edge 设备. 在data colelctor 机器配置并配置了edge server 地址(主要需要网络可访问) ...

  7. poj3311 经典tsp问题

    题目的大概意思就是一个人到一些城市送披萨,要求找到一条路径可以遍历每个城市后返回出发点,而且路径距离最短.最后输出最短距离就可以. 注意:每个城市可反复訪问多次. 因为题中明白说了两个城市间的直接可达 ...

  8. am335x_y蜂鸣器驱动

    修改文件:1.板级文件/arch/arm/mach-omap2/board-am335xevm.c static struct platform_device buzzer_device= { .na ...

  9. Linux下利用Shell使PHP并发采集淘宝产品

    上次项目中用到<<PHP采集淘宝商品>> 此方法有一个缺点,就是执行效率问题.一个商品采集平均需要0.8秒.那10000个商品采集完需要2个半小时. 首先想到的解决办法是并发. ...

  10. c# 爬虫(三) 文件上传

    在上一篇中,我们说了模拟登录, 下面我们说说附件上传. 据说,最早的http协议是不支持附件上传的,后来有添加了一个RFC 2045 协议,才支持附件上传,关于附件上传,请参见 http://www. ...