LeetCode: 310. Minimum Height Trees

MTK-AWB 小结 (二) 调试总结:可以将offset 对应的ratio加大,保留更多的喜好色(偏色),影响WF statistic gain,从而影响最终的awb gain。这一部分前期CCT校准的时候就会算出一组默认值,当然这个offset offse 值用于优化T,S,wf 光下的偏色喜好度. offset。后面遇到 A H Shad 光收白或者偏色,还可以优化 A 的offset ,H offset,shad offset.new offset 计算过程(Day locus offset 和M/G offset ) 阅读详情

LeetCode: 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 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 :

Input: n = 4, edges = [[1, 0], [1, 2], [1, 3]]

        0
        |
        1
       / \
      2   3 

Output: [1]

Example 2 :

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

     0  1  2
      \ | /
        3
        |
        4
        |
        5 

Output: [3, 4]

Note:

  • 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.”
  • The height of a rooted tree is the number of edges on the longest downward path between the root and a leaf.

解题思路

  1. 记,度为 1 的节点为叶子节点。显而易见从叶子节点到叶子节点的距离一定大于它们之间的节点分别到两个叶子节点的距离。因此,
  2. 当存在非叶子节点时,叶子节点一定不是 MHT 的根
  3. 当仅存在叶子节点时,所有的叶子节点都可以是 MHT 的根
  4. 综上,将原图的叶子节点一层一层的删除,直到满足 3 中的条件,剩下的所有的节点都可以是 MHT 的根

AC 代码

func countLeaves(degrees []int, nodes []int) (innerNodes []int, leaves []int) {
    for i := 0; i < len(nodes); i++ {
        if degrees[nodes[i]] == 1 {
            leaves = append(leaves, nodes[i])
        } else {
            innerNodes = append(innerNodes, nodes[i])
        }
    }
    
    return innerNodes, leaves
}

func findMinHeightTrees(n int, edges [][]int) []int {
    
    // 邻接矩阵: 每一行第一个值表示该行能到达的节点的总数
    adjMatrix := make([][]bool, n)
    for i := 0; i < n; i++ {
        adjMatrix[i] = make([]bool, n+1)
    }
    
    // 每个节点的度
    degrees := make([]int, n)
    for i := 0; i < len(edges); i++ {
        adjMatrix[edges[i][0]][edges[i][1]] = true
        adjMatrix[edges[i][1]][edges[i][0]] = true
        degrees[edges[i][0]]++
        degrees[edges[i][1]]++
    }
    
    
    var nodes []int
    for i := 0; i < n; i++ {
        nodes = append(nodes, i)
    }
    for {
        innerNodes, leaves := countLeaves(degrees, nodes)
        if len(innerNodes) == len(nodes) { 
            break 
        } else if len(innerNodes) == 0 {
            nodes = leaves
            break
        }

        for i := 0; i < len(leaves); i++ {
            for j := 0; j < len(adjMatrix[leaves[i]]); j++ {
                // 找到叶子唯一的那个度,并删除它
                if adjMatrix[leaves[i]][j] {
                    adjMatrix[leaves[i]][j] = false
                    adjMatrix[j][leaves[i]] = false
                    degrees[leaves[i]]--
                    degrees[j]--
                    break
                }
            }
        }
    
        nodes = innerNodes
    }
    
    return nodes
}
群晖如何安装软路由OpenWrt/iStoreOS作为旁路由(含网络设置、设置密码、还原出厂设置等linux命令ipk包) openwrt的固件。 阅读详情

相关推荐

如何用Python实现SPEI指数计算?从数据准备到结果可视化完整指南

本文提供了一份完整的Python实现SPEI指数计算指南,涵盖从数据准备、潜在蒸散量计算到结果可视化的全流程。详细介绍了使用Thornthwaite方法计算PET、拟合三参数log-logistic分布以及标准化转换的核心算法,并提供了模块化代码和可视化方案,帮助用户构建可复用的干旱分析工具。

cola5的博客 274

Leetcode: 310.Minimum Height Trees

Leetcode: 310.Minimum Height Trees

qq_16318319的博客 330

HyperLynx实战:手把手教你搞定DDR4信号完整性仿真(附ODT与终端电阻配置避坑)

本文详细介绍了使用HyperLynx进行DDR4信号完整性仿真的实战技巧,重点解析了ODT配置与终端电阻优化的黄金法则。通过模型准备、拓扑构建、ODT扫描等关键步骤,帮助硬件工程师规避常见设计陷阱,提升高速数字电路设计的效率与可靠性。

weixin_30861797的博客 650

leetcode 310 : Minimum Height Trees

1、原题如下: 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 ca

l3368bcttqnqn的博客 540

Java实战:用Proj4j搞定WGS84与CGCS2000坐标互转(附完整代码)

本文详细介绍了如何使用Java库Proj4j实现WGS84与CGCS2000坐标系之间的高精度转换。通过七参数转换模型和格网改正量文件,提供完整的代码实现和项目配置指南,帮助开发者快速集成到地理信息系统项目中,提升坐标转换效率和精度。

weixin_29251567的博客 835

OCC入门指南:从零开始掌握Open CASCADE Technology

本文是Open CASCADE Technology(OCC)的入门指南,详细介绍了这款开源C++三维几何建模内核的核心概念与价值。内容涵盖从零开始搭建开发环境、编写第一个显示立方体的程序,到深入理解其几何与拓扑核心、掌握Draw测试工具及Handle内存管理等关键编程范式,旨在帮助开发者快速掌握这一工业级CAD/CAE/CAM应用开发利器。

java5的博客 1279

草稿:Leetcode 310. Minimum Height Trees

这个思路是先构建一个树,然后不断地移动根节点的子节点到根节点的位置,但这个过程中需要实现的部分感觉很多,写了一半写不下去了,下面是未完成的草稿 import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Queue; import java

独钓寒江雪 346

双击文件夹弹窗“找不到应用程序”,但是右键点击打开可以

此电脑、回收站、c、d、f盘、控制面板、任意文件夹等都没有办法双击打开。 右键中包含“使用filemanager打开”,是由于FileManager卸载残留导致的。 懒人专用附件 下载解压上面附件,双击导入可以清理FileManager软件残留 参考: 双击此电脑出现找不到应用程序,但是右键点击可以-其他-CSDN问答https://ask.csdn.net/questions/7631439 电脑上的文件夹双击都提示找不到应用程序,但是右击打开就能打开,该怎么解决? - 知乎 (z

webmasterqpwou的博客 1万+

LeetCode:310. Minimum Height Trees

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

lllllyt的博客 479

C# 调用高德地图关键词 API

今天我们要一起探索如何使用 C# 来调用高德地图的关键词搜索 API,从而实现精准的地理位置搜索功能。高德地图的关键词搜索 API 可以帮助我们轻松地实现这一点,它能够返回一系列匹配关键词的地点坐标和其他相关信息。通过简单的几行 C# 代码,我们就实现了调用高德地图关键词搜索 API 的功能。接下来,我们将在一个 C# 控制台应用中实现调用高德地图关键词搜索 API 的功能。此外,如果要在生产环境中使用,还需要考虑安全性问题,例如不要在客户端暴露 API 密钥。🔥关注墨瑾轩,带你探索编程的奥秘!

java专栏 1110

FLUENT Meshing Watertight Geometry工作流入门 - 9 生成体网格

总结一下我们学到的东西。讨论体积网格的重要性,并了解生成体积网格的不同方法。还了解体积网格质量的重要性,并学习如何改进它。

B_etter_Man的博客 1万+

LeetCode Graph:M310. 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

iyangdi的博客 329

基于Simulink的间接功率控制的矩阵变换器

通过上述步骤,我们成功设计并实现了基于 Simulink 的间接功率控制的矩阵变换器仿真模型。该模型能够通过控制输入电流和输出电压,实现对功率的间接控制,适用于电机驱动、可再生能源系统、电力传输等领域。通过虚拟场景仿真、参数优化和性能评估,验证了系统的稳定性和高效性,并通过调整 PI 控制器参数和优化调制算法,进一步提升了系统的性能。

MHD0815的博客 302

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

weixin_30753873的博客 129

蓝牙模块HM-10手册

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

全球国家地区城市数据

MySQL全球国家地区城市数据报。。。。。。。。。。。。

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

Ruby超时算法: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] = [

Fourier_xyz的博客 195

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

[算法分析与设计] leetcode 每周一题: 310. Minimum Height Trees

题目链接:  310. Minimum Height Trees 题目大意: 给定一个具有树的性质的无向图, 选择其中任意一个节点作为根节点即可生成一棵有根树, 所有不同的生成树中, 高度最小的那些便被称为 minimum height trees (MHTs), 现要求找出 给定无向图的 MHTs, 返回结果为 各MHT 的根节点(对应的 label) ;

sinat_37893507的博客 404

leetcode_c++:图:Minimum Height Trees(310)

class Solution { public: vector<int> findMinHeightTrees(int n, vector<pair<int, int>>& edges) { //corner case if ( n <= 1 ) return {0}; //construct a edges search data stuctu

LandscapeMi 579

[LeetCode] 310. Minimum Height Trees_Medium tag: BFS

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

weixin_30271335的博客 123

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

LeetCode //C - 310. Minimum Height Trees

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

Made in Code 1248

[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

coder 进阶的专栏 4538

Leetcode 310. Minimum Height Trees

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

独钓寒江雪 743

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

原题网址:https://leetcode.com/problems/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 po

James Pan 3128

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(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: 309. Best Time to Buy and Sell Stock with Cooldown
下一篇: LeetCode: 312. Burst Balloons
IT技术小密圈
博客等级 码龄12年 1385粉丝 322原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值