[LeetCode 310] Minimum Height Trees

手把手教你用家用电脑完成图片和视频AI去水印功能 图片视频 AI去水印 阅读详情

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]

Hint:

  1. How many MHTs can a graph have at most?

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.

Solution:

build graph first, then find leaf and remove them among their neighbors, level by level. Until left less 2 nodes

public List<Integer> findMinHeightTrees(int n, int[][] edges) {
        List<Integer> leaf = new ArrayList<>();
        if(n<=1) {
            leaf.add(0);
            return leaf;
        }
        Map<Integer, List<Integer>> graph = new HashMap<>();
        for(int i=0;i<n;i++) graph.put(i, new ArrayList<>());
        int[] neighbors = new int[n];
        for(int[] edge : edges) {
            neighbors[edge[0]]++;
            neighbors[edge[1]]++;
            graph.get(edge[0]).add(edge[1]);
            graph.get(edge[1]).add(edge[0]);
        }
        
        for(int i=0;i<n;i++) {
            if(graph.get(i).size() ==1 ) leaf.add(i);
        }
        while(n>2) {
            List<Integer> newLeaf = new ArrayList<>();
            for(int l : leaf) {
                n--;
                for(int nb : graph.get(l)) {
                    if(--neighbors[nb] == 1) newLeaf.add(nb);
                }
            }
            leaf = newLeaf;
        }
        return leaf;
    }




学习STM32的CAN总线通信 在配置时,确保您启用了CAN总线功能。CAN节点是CAN总线上的一个设备,每个节点都有唯一的标识符。在CAN总线中,有两种类型的节点:CAN控制器和CAN外设。步骤2:配置CAN总线 在STM32CubeMX中,选择CAN总线模块并进行必要的配置。在CAN总线中,有两个重要的概念:CAN节点和CAN帧。步骤5:测试CAN总线通信 在成功编译和烧录代码后,您可以使用串口调试助手或其他CAN总线工具来测试CAN总线通信。步骤3:编写CAN总线通信代码 在项目的主代码文件中,我们将编写CAN总线通信的示例代码。 阅读详情

相关推荐

智能音箱开发者必看:CEVA-BX vs Cadence HiFi音频DSP实战选型指南(附功耗对比表)

本文深入对比了CEVA-BX和Cadence HiFi两大音频DSP架构在智能音箱开发中的性能差异,涵盖语音唤醒、多麦克风阵列处理等核心场景。通过实测数据和功耗对比表,为开发者提供选型参考,帮助优化产品性能和能效表现。

weixin_29309213的博客 232

LeetCode-310. 最小高度树【dfs,bfs,拓扑排序】

LeetCode-310. 最小高度树【dfs,bfs,拓扑排序】

zik的博客 765

电源新手别怕!手把手教你搞定Buck电路的环路补偿(附Type II/III补偿器参数计算)

本文为电源设计新手详细解析Buck电路环路补偿技术,重点介绍Type II和Type III补偿器的参数计算与设计方法。通过实例演示和实用技巧,帮助工程师快速掌握环路补偿原理,解决电源振荡、响应慢等问题,提升电路稳定性与性能。

weixin_30607029的博客 641

leetcode 310. Minimum Height Trees(最小高度树)

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. Given a tree of n nodes labelled from 0 to n - 1, and an array of n - 1 edges where edges[i] = [

蓝羽飞鸟的博客 724

(南京观海微电子)——TCON介绍

TCON又称:逻辑板,控制板,在液晶电视里的作用和CRT中的视放板相当,但有本质的区别,逻辑板不是一个纯粹的信号放大器,它输入是LVDS格式信号,而不是RGB。逻辑板的作用是把数字板送来的LVDS或TTL图像数据信号,时钟信号进行处理移位寄存器存储将图像数据信号,时钟信号转换成屏能够识别的控制信号行列信号RSDS控制屏内的MOSFET管工作而控制液晶分子的扭曲度。目前国内主要的TCON板生产商有视显光电。传统TCON的布局主要分为以下两种:X+C分离式和X+C一体式。

m0_75095200的博客 3845

TP4056在单节锂电池充电管理中的创新应用与电路优化

本文深入探讨了TP4056线性充电管理芯片在单节锂电池充电电路中的创新应用与优化方案。针对“边充边用”的核心挑战,文章详细解析了利用PMOS管构建智能供电切换电路的原理,实现了外部电源与电池供电的无缝、安全切换,有效杜绝了电池“边充边放”的风险。同时,提供了关键的元器件选型、PCB布局布线技巧及常见问题排查指南,为创客和硬件开发者提供了高性价比、高可靠性的锂电池电源管理实践方案。

rock5的博客 506

310. Minimum Height Trees

For an 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 fu

lianggx3的博客 192

R语言中的summarise函数和mutate函数用于计算分组占比。下面是关于这两个函数的详细介绍以及相应的源代码示例:

R语言中的summarise函数和mutate函数用于计算分组占比。

CodeByte的博客 1186

leetcode310 最小高度树 题解

大致意思是给你一个树,然后让你选择一些点作为根,使得树的高度最小。

ZXTpower的博客 1494

3D 结构光相机工作原理全解 | 从编码投射到三维重建,原理 + 代码 + 工业实战一站式掌握

本文从3D结构光相机的整体架构出发,层层拆解了编码投射、相位计算、相位解包裹、三维重建的核心原理,补充了工业实战所需的公式推导、代码实现、参数调优、设备选型和案例落地,核心总结如下:相位是3D结构光成像的“灵魂”,其精度、稳定性直接决定3D重建的效果,包裹相位与绝对相位的区分的是工业实战的基础;编码投射的核心是“正弦条纹+格雷码条纹”组合,正弦条纹负责高精度相位提取,格雷码条纹辅助解包裹,适配工业场景的精度与稳定性需求;

getusushu的博客 418

某手短视频用户主页视频列表爬虫实战:从原理到完整实现

随着短视频平台的迅猛发展,某手(为保护平台隐私,本文以“某手”代指实际目标)已经成为国内最大的短视频社交平台之一,拥有数亿日活用户。对于数据分析师、内容创作者和市场营销人员而言,获取某个用户主页的视频列表具有重要的研究价值——无论是分析头部创作者的内容策略,还是监测竞品的发布频率与互动数据,爬虫技术都成为不可或缺的工具。然而,在2024-2025年的技术环境下,某手已经构建了多层次的反爬机制:包括接口签名加密、请求参数动态混淆、设备指纹检测、IP行为分析以及Web端无限滚动加载的异步数据获取方式。

2201_76125261的博客 549

Leetcode 310. Minimum Height Trees

310. Minimum Height Trees For an 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 min...

大洋深处 279

分布式训练中All-Reduce、All-Gather、Reduce-Scatter原理

Reduce-Scatter = 先对所有进程的数据做 Reduce(如 sum / mean),再把结果“切分”分发给各进程。(是 Reduce + Scatter 的融合操作。

comli_cn的博客 349

[leetcode] 310. Minimum Height Trees

Description For an 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 ar...

Learning from the mistakes 263

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

InTheHand.Net.Personal+BlueTooth4,蓝牙开发

项目中有蓝牙开发的必备dll,现在网上相关的文档博客不少,有的说的多有的说少,但是却很少有这两个dll,分享一下。

Leetcode 310. Minimum Height Trees

Leetcode  310. Minimum Height Trees 这个解法思路很巧妙: 从最简单地开始:如果是一个list,从两端采用双指针的方法,当两个指针相遇或间隔为1时返回。 如果是一个树的话,则从度为1的节点(不一定是叶子节点),度为一只需要统计边数就可以了,多个指针向前遍历(宽度优先搜索),直到只剩一个或两个节点,便为所求。 import java.

独钓寒江雪 743

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

LeetCode 310. Minimum Height Trees

图中的路径

Llunnn的博客 510

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

[LeetCode]310. Minimum Height Trees

Minimum Height Trees

LadyCaro的博客 390

leetcode 310. Minimum Height Trees

题目是给了一个图,求把某个节点提起来后,形成的树的高度最小。 第一个想法就是暴力搜索。 把每个点提起来,然后计算并对比树的高度。 结果就是超时。 优化了一些,发现还是没有办法在规定时间内处理完。 看了网上的大神huifeng guan和残酷刷题群之前的解法,觉得自己太low了。 既然题目要求提起某个节点后树的高度最小,问题转换下就是某个节点提起来后到其余的每个节点的距离最短。 我的高中的楼布局就是和这道题的图差不多,高一在北美的楼,高二,高三...

328

Leetcode 310. Minimum Height Trees(python)

Leetcode 310. Minimum Height Trees题目解法:拓扑排序+BFS 题目 For an undirected graph with tree characteristics, we can choose any node as the root. The result graph is then a rooted tree. Among all possible roo...

qq_37821701的博客 506

LeetCode //C - 310. Minimum Height Trees

【代码】LeetCode //C - 310. Minimum Height Trees。

Made in Code 1248

力扣 310. Minimum Height Trees

一、简述   这道题本质就是求树的中心,我没有能够独立做出这道题。 二、思想   首先我们证明树的中心一定位于这个有向无环图的某一条最长路径的中点上。   a) 第一步证明其他任意节点到这条最长路径两端距离的最大值一定大于等于最长路径的中点到这条最长路径两端距离的最大值。这意味着以其他任意节点为根的树的height一定大于最长路径的中点到这条最长路径两端距离的最大值。   b) 第二步能够证明最长路径的中点到任意节点的距离一定小于等于最长路径的中点到这条最长路径两端距离的最大值。通过简单的反证法,能够证明若

wZ2zY的博客 749

LeetCode-310. Minimum Height Trees [C++][Java]

When you select a nodexas the root, the result tree has heighth. Among all possible rooted trees, those with minimum height (i.e.min(h)) are calledminimum height trees(MHTs). Returna list of allMHTs'root labels.

贫道绝缘子的博客 568

Python 2移植实战案例:SVD-Loader-Ghidra如何把cmsis-svd库适配到Ghidra的Jython环境

SVD-Loader-Ghidra 是一个免费的 Ghidra 脚本项目,用于加载 CMSIS-SVD 文件并在逆向分析中自动生成外设内存块与寄存器结构体。它的核心看点是一次典型的 **Python 2 移植实战**:作者把上游面向 Python 3 的 `cmsis-svd` 解析库改造(fork)后,成功适配到 Ghidra 内置的 **Jython**(Python 2 on JVM)环境中

gitblog_00280的博客 324

LeetCode 310. Minimum Height Trees 解法

LeetCode 310. Minimum Height Trees 解法 最简单的解法就是DFS遍历,即从每个节点出发,计算到达末端节点的最大路径长度,那么此路径长度最小的就是最矮树的根(可能有多个)。但是这个方法会超时,代码如下(Java): class Solution { public List<Integer> findMinHeightTrees(int n, in...

ZZXN的博客 322

leetcode-310. Minimum Height Trees

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 mini

qj30212的博客 446

Leetcode 310 Minimum Height Trees

最近在刷leetcode了。。都是medium和easy。。 今天第一次碰到树遍历的题目。。超时了 310. Minimum Height Trees public List findMinHeightTrees(int n, int[][] edges) { nn=n; List list=new LinkedList; if(n==0){

qq_27262609的博客 426
上一篇: [LeetCode 319] Bulb switch
下一篇: [LeetCode 313] Super Ugly Number
Mavs
博客等级 码龄17年 166粉丝 296原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值