LeetCode Graph:M310. Minimum Height Trees

开发区管委会想建立公共技术服务平台体系,在成果治理机制上该如何规划?.docx 开发区管委会想建立公共技术服务平台体系,在成果治理机制上该如何规划? 立即下载

For a undirected graph with tree characteristics, we can choose any node as the root. The result graph is then a rooted tree. Among all possible rooted trees, those with minimum height are called minimum height trees (MHTs). Given such a graph, write a function to find all the MHTs and return a list of their root labels.

Format
The graph contains n nodes which are labeled from 0 to n - 1. You will be given the number n and a list of undirected edges (each edge is a pair of labels).

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.

Example 1:

Given n = 4, edges = [[1, 0], [1, 2], [1, 3]]

        0
        |
        1
       / \
      2   3

return [1]

Example 2:

Given n = 6, edges = [[0, 3], [1, 3], [2, 3], [4, 3], [5, 4]]

     0  1  2
      \ | /
        3
        |
        4
        |
        5

return [3, 4]

Note:

(1) According to the definition of tree on Wikipedia: “a tree is an undirected graph in which any two vertices are connected by exactly one path. In other words, any connected graph without simple cycles is a tree.”

(2) The height of a rooted tree is the number of edges on the longest downward path between the root and a leaf.

Credits:
Special thanks to @dietpepsi for adding this problem and creating all test cases.

//一般给的是边,不方便用BFS,更适合拓扑
//利用拓扑的性质
public class Solution {
  int[][] es;
  public List<Integer> findMinHeightTrees(int n, int[][] edges) {
      es = edges;
      List<Integer> res = new ArrayList<Integer>();
      
      if(n==0) return res;
      if(n==1){
          res.add(0);
          return res;
      }
      
      //n>1        
      int m = es.length;//边的个数
      int[] nums = new int[n];//保存连接到点i上的点的个数
      
      //计算连接到点i上的点的个数
      for(int i=0; i<m; i++){
          nums[es[i][0]]++;
          nums[es[i][1]]++;
      }
      
      int count = 0;
      while(true){
          //找到所有的叶子,度=1            
          Queue<Integer> q = new LinkedList<Integer>();
          for(int i=0; i<n; i++){
              if(nums[i]==1){
                  q.add(i);
                  count++;
                  nums[i]=-1;
              }else if(nums[i]==0){
                  res.add(i);
                  return res;
              }                
          }
          
          //最后
          if(count==n){
              while(!q.isEmpty())
                  res.add(q.poll());
              return res;
          }
          
          //删除所有的叶子
          while(!q.isEmpty()){                
              int t = q.poll();
              for(int i=0; i<m; i++){
                  int a = es[i][0];
                  int b = es[i][1];
                  if((a==t || b==t)){
                	  int tmp;
                      if(a==t)
                          tmp = b;
                      else
                    	  tmp = a ;
                      if(nums[tmp]!=-1){
                          nums[tmp]--; 
                          break;
                      }
                  }                            
              } 
          }           
      }
  }
}

省级技术交易市场目前的年均交易额规模和增长趋势如何?.docx 省级技术交易市场目前的年均交易额规模和增长趋势如何? 立即下载

相关推荐

《WPF 列表视图(ListView)控件详解与使用-WPF 中的控件(22)-列表视图(ListView)》一文的工程文件

《WPF 列表视图(ListView)控件详解与使用—WPF 中的控件(22)--列表视图(ListView)》一文的工程文件

Leetcode310. Minimum Height Trees

A tree is an undirected graph in which any two vertices are connected byexactlyone path. In other words, any connected graph without simple cycles is a tree. Given a tree ofnnodeslabelled from0ton - 1, and an array ofn - 1edgeswhereedges[i] = ...

weixin_49991368的博客 233

华大HC32F460 ADC+DMA高效数据采集实战解析

本文深入解析了华大HC32F460微控制器中ADC与DMA协同工作的高效数据采集方案。通过实战代码演示,详细阐述了如何配置硬件触发、序列扫描及AOS事件联动,实现后台自动采集,从而解放CPU资源,提升系统实时性与可靠性,适用于传感器数据采集、音频处理等多种场景。

weixin_29189373的博客 184

Google算法题:M-求二叉搜索树的第K小的数

http://www.jiuzhang.com/article/Google%202016%E5%B9%B412%E6%9C%88SDE%E7%94%B5%E9%9D%A2%E9%9D%A2%E7%BB%8F/ 给出一个二叉搜索树,写出一个方法,求出树中的第k小的数 //中序遍历-递归 int k; public int findTheKth(TreeNode root

iyangdi的博客 666

源码编译部署篇(二)源码编译milvus成功后如何启动standalone并调试成功!

由于Milvus官方文档只提及如何使用docker安装部署Milvus,关于源码安装方式篇幅较少,因此,我整理撰写了如何通过源码编译方式成功安装Milvus,详见此篇。之所以选择这种复杂的安装方式,可以调试源码进而熟悉Milvus如何搭建整个框架支持AI上层应用功能。根据上篇步骤部署虽然可以成功,然而,我却不能成功启动Milvus服务,这个问题困扰了一两天,现在终于得以解决并能成功调试Milvus源码。接下来,分为三个部分介绍如何启动和成功调试。

qq_43893755的博客 3375

LoRa模块选型指南:正点原子ATK-LORA-01与SX1278芯片深度评测(含实测距离/功耗数据)

本文深度评测了正点原子ATK-LORA-01与SX1278芯片的性能表现,通过实测数据展示其在复杂环境中的通信距离与功耗表现。特别适合智慧农业、工业监测等物联网应用场景,提供详细的配置指南和优化技巧,帮助开发者实现高效、低功耗的LoRa通信解决方案。

weixin_29191081的博客 300

LeetCode:M-139. Word Break

LeetCode链接 Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictiona

iyangdi的博客 476

14、X射线图像处理与特征提取全解析

本文全面解析了X射线图像处理与特征提取的关键技术,涵盖图像预处理、滤波、边缘检测、分割与恢复等流程,并深入探讨了几何特征(如面积、周长、圆度、椭圆拟合)、强度特征(均值、方差、对比度)、傅里叶描述符、不变矩(Hu矩、Gupta矩)以及局部描述符(SIFT、SURF)和稀疏表示方法。同时介绍了特征选择的过滤法、包装法和嵌入法,并结合Matlab代码示例展示了各类特征的实际提取过程。文章最后总结了当前技术的应用现状,并展望了深度学习在X射线图像分析中的未来潜力。

pz89012345的博客 139

LeetCode:M-647. Palindromic Substrings

LeetCode链接 given a string, your task is to count how many palindromic substrings in this string. The substrings with different start indexes or end indexes are counted as different subst

iyangdi的博客 468

解决TexStudio编写LaTeX论文引用文献问题:正文引用处出现[?],参考文献也不显现

当我们利用LaTeX编写论文时,可能会出现引用参考文献时正文引用处出现?号,肯能的原因及解决方案如下: 问题一 忘记在\end{document}前面放\bibliographystyle{样式}和\bibliography{参考文献bib文件名}: \bibliographystyle{plain} \bibliography{sample} 其中, LaTeX 标准选项及其样式共有以下8种: plain,按字母的顺序排列,比较次序为作者、年度和标题. unsrt,样式同plain,只是按照引用的先后

左手の明天的博客 9536

AXI_Bus_Matrix_4x4 设计

Signal Source Width(bits) Description AWID Master AWID_WIDTH 写事务ID AWADDR Master

Starry__的博客 3775

LeetCode:M-547. Friend Circles

LeetCode链接 There are N students in a class. Some of them are friends, while some are not. Their friendship is transitive in nature. For example, if A is a direct friend of B, and B is a direc

iyangdi的博客 456

华为OD机试2025C卷 - 精准核酸检测 (C++ & Python & JAVA & JS & GO)

华为OD机试2025C卷 精准核酸检测,提供Java、C++、Python、Go、JavaScript源码实现,并提供完整思路讲解

qq_45776114的博客 1711

LeetCode:M-105. Construct Binary Tree from Preorder and Inorder Traversal

LeetCode链接 Given preorder and inorder traversal of a tree, construct the binary tree. /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left;

iyangdi的博客 448

Linux ARM平台开发系列讲解(GPIO子系统) 2.3.2 GPIO子系统常用函数解析

1. 概述 该章节会记录一年常用的GPIO子系统常用函数,会更具情况不断更新 static inline int gpio_get_value(unsigned int gpio) 功能 :用于获取某个GPIO引脚上的值 gpio : gpio引脚号 返回值:非负值,得到的 GPIO 值;负值,获取失败。 static inline void gpio_set_value(unsigned int gpio, int value) static inline int gpio_cansleep(unsi

DSMGUOGUO的博客 675

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

LeetCode:M-583. Delete Operation for Two Strings

LeetCode链接 Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 the same, where in each step you can delete one character in either string. Ex

iyangdi的博客 445

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

Google/LintCode:M-图是否是树

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

iyangdi的博客 443

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

LeetCode:M-279. Perfect Squares

LeetCode链接 Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n. For example, given n = 12, return 3 because 12 =

iyangdi的博客 402

LeetCode:M-287. Find the Duplicate Number

LeetCode链接 Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one

iyangdi的博客 389

LeetCode:M-565. Array Nesting

LeetCode链接 A zero-indexed array A consisting of N different integers is given. The array contains all integers in the range [0, N - 1]. Sets S[K] for 0 S[K] = { A[K], A[A[K]], A[A[A[K]

iyangdi的博客 386

Google/LintCode:M-寻找峰值

题目 题目来源:Link 你给出一个整数数组(size为n),其具有以下特点: 相邻位置的数字是不同的A[0] A[n - 1] 假定P是峰值的位置则满足A[P] > A[P-1]且A[P] > A[P+1],返回数组中任意一个峰值的位置。  注意事项 数组可能包含多个峰值,只需找到其中的任何一个即可 您在真实的面试中

iyangdi的博客 358

Google面试题:M-Fraction to Recurring Decimal

http://www.jiuzhang.com/article/Google2014Experience%20%E8%BD%AF%E4%BB%B6%E5%B7%A5%E7%A8%8B%E5%B8%88onsite%EF%BC%883%EF%BC%89/ http://www.lintcode.com/zh-cn/problem/add-operators/ Given tw

iyangdi的博客 357

LeetCode Graph:M399. Evaluate Division

Equations are given in the format A / B = k, where A and B are variables represented as strings, and k is a real number (floating point number). Given some queries, return the answers. If the answ

iyangdi的博客 352

LeetCode:M-560. Subarray Sum Equals K

LeetCode链接 Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k. Example 1: Input:nums = [1,1,1], k = 2 Output: 2

iyangdi的博客 340

Google算法题:H-Shortest Palindrome

http://www.jiuzhang.com/article/Google%202016%E5%B9%B412%E6%9C%88%20SDE%20Onsite%E9%9D%A2%E7%BB%8F/ https://leetcode.com/problems/shortest-palindrome/description/ Given a string S, you a

iyangdi的博客 335
上一篇: LeetCode Graph:M210. Course Schedule II
下一篇: LeetCode Graph:M332. Reconstruct Itinerary
iyangdi
博客等级 码龄12年 326粉丝 160原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值