图是否是树-LintCode

204 篇文章 0 订阅

给出 n 个节点,标号分别从 0 到 n - 1 并且给出一个 无向 边的列表 (给出每条边的两个顶点), 写一个函数去判断这张`无向`图是否是一棵树
注意事项:
你可以假设我们不会给出重复的边在边的列表当中. 无向边 [0, 1] 和 [1, 0] 是同一条边, 因此他们不会同时出现在我们给你的边的列表当中
样例:
给出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.

#ifndef C178_H
#define C178_H
#include<iostream>
#include<vector>
using namespace std;
class Solution {
public:
    /**
    * @param n: An integer
    * @param edges: a list of undirected edges
    * @return: true if it's a valid tree, or false
    */
    bool validTree(int n, vector<vector<int>> &edges) {
        // write your code here
        int len = edges.size();
        if (len != n - 1)
            return false;
        vector<int> nums;
        for (int i = 0; i < n; ++i)
            nums.push_back(i);
        for (auto c : edges)
        {
            int root1 = unionSearch(c[0], nums);
            int root2 = unionSearch(c[1], nums);
            //若最顶层的节点相同则出现环,不是树
            if (root1 == root2)
                return false;
            nums[root1] = root2;
        }
        return true;
    }
    //找到root最高层的点
    int unionSearch(int root, vector<int> &nums)
    {
        while (root != nums[root])
        {
            nums[root] = nums[nums[root]];
            root = nums[root];
        }
        return root;
    }
};
#endif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值