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

YOLOv11 模型剪枝实战:深入解析通道剪枝与层剪枝的策略与实现(含环境配置、微调技巧与部署测试) YOLOv11模型剪枝技术通过移除冗余参数和结构来压缩模型,主要包括通道剪枝和层剪枝两种方法。通道剪枝通过评估批量归一化层的缩放因子γ确定通道重要性,移除不重要的特征通道;层剪枝则直接删除整个网络层或模块。剪枝流程包括评估重要性、移除冗余成分、微调模型和评估性能四个步骤。该技术可显著降低模型计算量和参数量,适合在资源受限设备上部署高性能目标检测模型。 阅读详情

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] = [ai, bi] indicates that there is an undirected edge between the two nodes ai and bi in the tree, you can choose any node of the tree as the root. When you select a node x as the root, the result tree has height h. Among all possible rooted trees, those with minimum height (i.e. min(h)) are called minimum height trees (MHTs).

Return a list of all MHTs’ root labels. You can return the answer in any order.

The height of a rooted tree is the number of edges on the longest downward path between the root and a leaf.
在这里插入图片描述
一个无向图表示的树,有n个节点(0到n-1),边edges = [ai, bi]表示第i条边的两个端点是ai, bi。
选任一节点作为root, 可得到一个树的高度。
返回可以得到最小高度的所有root

思路:
可以用拓扑排序,先介绍下拓扑排序。参考链接
在这里插入图片描述
这里同样选一个入度为0的node,一层一层删掉,最后剩<=2个node时结束,剩下的node就是我们需要的root

为什么要这样做?因为每次删掉的都是入度为0(最外层)的node,剩下的是处于最中间的node
为什么要选剩下的<=2个节点?剩一个节点时,它肯定是最中间的root。剩两个时,两个谁做root,高度都是一样的。当剩3个时,由于树中不存在环,肯定有一个高度是大于另外两个的。

具体实现步骤:

  1. 将输入的边edges构建成图,注意无向图要双向
  2. 找到入度为0(即仅有一个边)的node,放入queue
  3. 由于是无向图,需要从边的另一端点对应的连接node表中,删除当前node,删除完后,如果只剩下一个边,装入queue,作为下一轮删除对象
  4. 直到queue中只剩下<=2个节点,返回
    public List<Integer> findMinHeightTrees(int n, int[][] edges) {
        List<Integer> roots = new ArrayList<>();
        if(n == 0 || edges == null) return roots;
        
        if(n == 1) {
            roots.add(0);
            return roots;
        }
        if(n == 2) {
            roots.add(0);
            roots.add(1);
            return roots;
        }
        
        //HashSet便于删除node
        List<Set<Integer>> graph = new ArrayList<>(n);
        
        for(int i = 0; i < n; i++) {
            graph.add(new HashSet<Integer>());
        }
        
        //无向图
        for(int[] edge : edges) {
            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) {
                roots.add(i);
            }
        }
        
        while(n > 2) {
            n -= roots.size();  //遍历下来roots里面的节点都要删除
            List<Integer> newRoots = new ArrayList<>(); //保存新的叶子节点
            for(Integer root : roots) {
                int node = graph.get(root).iterator().next();
                graph.get(node).remove(root);
                if(graph.get(node).size() == 1) newRoots.add(node);
            }
            roots = newRoots;
        }
        return roots;
        
    }

上面的方法有点耗时,主要是在于要从边的另一端点所对应的表中删除当前node。其实我们想要的只是看入度是否为0(是否只有一条边),即边的另一端点所对应的表的size是否为1。并不关心每个点具体和哪个点相连接。

可以建立一个数组,记录每个node有几条边,当只有一条边时加入queue,就不需要再反复地删除节点了。

以下是错误做法,因为如果没有新添加入度为0的节点,而queue中的节点仅剩两个的时候,就会跳出循环,queue中保存的之前的节点会处理不完

while(queue.size() > 2) {
            int deleteNode = queue.poll();
            //由于没有节点删除这一步了,要遍历整个连接点的表
            int deleteConnectNode = graph[deleteNode].get(0); //入度为0,肯定只连接一个点
            degree[deleteNode] --;
            degree[deleteConnectNode] --;
            if(degree[deleteConnectNode] == 1) queue.offer(deleteConnectNode);
        }

完整代码

    public List<Integer> findMinHeightTrees(int n, int[][] edges) {
        List<Integer> roots = new ArrayList<>();
        if(n == 0 || edges == null) return roots;
        
        if(n == 1) {
            roots.add(0);
            return roots;
        }
        if(n == 2) {
            roots.add(0);
            roots.add(1);
            return roots;
        }
        
        ArrayList<Integer>[] graph = new ArrayList[n];
        int[] degree = new int[n];
        Queue<Integer> queue = new LinkedList<Integer>();
        boolean[] removed = new boolean[n];
        
        for(int i = 0; i < n; i++) {
            graph[i] = new ArrayList<Integer>();
        }
        
        
        for(int[] edge : edges) {
            graph[edge[0]].add(edge[1]);
            graph[edge[1]].add(edge[0]);
            degree[edge[0]] ++;
            degree[edge[1]] ++;
        }
        
        for(int i = 0; i < n; i++) {
            if(degree[i] == 1) queue.offer(i); 
        }
        
        while(n > 2) {
            int size = queue.size();
            n -= size;
            while(size > 0) {
                int deleteNode = queue.poll();
                removed[deleteNode] = true;
                for(int deleteConnectNode : graph[deleteNode]) {
                    if(removed[deleteConnectNode]) continue;
                    degree[deleteConnectNode] --;
                    if(degree[deleteConnectNode] == 1) queue.offer(deleteConnectNode);
                }
                size --;
            }
        }
        
        roots.addAll(queue);
        return roots;
    }
多智能体系统在AI物流优化中的实际应用案例 订单波峰波谷难应对(如双11订单量暴增300%);动态干扰难处理(如突发交通拥堵、客户临时改地址);全局优化难实现(单辆车最优≠整体最优)。本文聚焦“多智能体系统(MAS)”这一AI技术,讲解其如何通过“群体智能”破解物流调度难题,并结合实际案例展示落地效果。本文从“生活故事→核心概念→技术原理→实战案例→未来趋势”逐步展开,用“快递员团队协作送外卖”类比MAS运作,结合Python代码模拟任务分配,最后通过某物流企业的真实项目验证效果。 阅读详情

相关推荐

Unity 2022打包Android APK:手把手教你创建和配置第一个签名Keystore(避坑指南)

本文详细介绍了Unity 2022打包Android APK时创建和配置签名Keystore的全流程,包括环境准备、Keystore创建、高级配置避坑指南以及签名验证与故障排查。特别强调了Android应用签名的重要性及安全维护策略,帮助开发者避免常见错误,确保应用顺利发布。

weixin_27379781的博客 249

最小高度树

力扣算法之最小高度树

qq_52916408的博客 1178

因内存不足原因导致fragment空白解决方案

在androidx之前,一般我们防止Fragment重建是在actvitity的onCreate方法中使用savedInstanceState?.remove(“android:support:fragments”)或者savedInstanceState?但是在androidx中,使用这两行代码不生效,但是看FragmentActivity中的FRAGMENT_TAG还是android:support:fragments,那为什么会不生效呢,后来点进去看发现。

qq_39431405的博客 550

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

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

zik的博客 765

手把手教你用imu_utils和imu_tk标定MEMS IMU(附避坑指南)

本文提供了一份在Linux/ROS环境下,使用imu_utils和imu_tk开源工具标定MEMS IMU的完整实战指南。内容涵盖环境搭建、数据采集技巧、确定性误差与随机性误差(Allan方差分析)的标定流程、结果解读以及详细的避坑经验,旨在帮助机器人开发者获得精确的传感器参数,提升系统状态估计的稳定性与精度。

root9的博客 459

远心F-theta镜ZEMAX设计

根据给出的F-theta镜的设计指标,对F-theta镜进行了具体的光学设计。从对F-theta镜设计思想的介绍出发,对选取的初始结构进行了优化设计,优化完成后,对所设计的F-theta镜进行了像质评价,通过点列图、场曲与畸变图、相对照度、能量集中度对所设计的F-theta镜进行了分析,最终,对F-theta镜的设计效果进行了验证。

hitliuyi17的博客 9471

310. Minimum Height Trees

本题给出一个有树性质的无向图,可以选择任何一个点做树根,要求在所有可能性中,找出树高最小的MHTs,并输出树根。 本题给出的图本身就是一棵树,不是一个最小生成树的问题。理论上求出每个节点对应的树高,然后排序即可,这样的复杂度在O(v*v),没有试过这样能不能过。 另一方面,也可以将问题转化来看,首先,先不考虑两个节点以下的情况,很显然图中入度为0的点形成的树一定不是MHTs;入度为1的点也不是,

zdinanyang的博客 450

Acgis中实现栅格经纬度和行政区关联

目前已有意大利的shp文件,希望将意大利划分成0.1*0.1经纬度的栅格,并且关联每个栅格中心点所属的省份信息。最终希望得到的输出是一个表格,里面包含栅格的经纬度、所属省份信息。

qq_42183184的博客 3375

力扣 310. 最小高度树

题目 树是一个无向图,其中任何两个顶点只通过一条路径连接。 换句话说,一个任何没有简单环路的连通图都是一棵树。 给你一棵包含 n 个节点的树,标记为 0 到 n - 1 。给定数字 n 和一个有 n - 1 条无向边的 edges 列表(每一个边都是一对标签),其中 edges[i] = [ai, bi] 表示树中节点 ai 和 bi 之间存在一条无向边。 可选择树中任何一个节点作为根。当选择节点 x 作为根节点时,设结果树的高度为 h 。在所有可能的树中,具有最小高度的树(即,min(h))被称为 最小高

(ง •_•)ง没有困难的工作,只有勇敢的狗狗 818

HR效率翻倍!用Dify+飞书AI表格打造自动化简历处理流水线(附完整代码)

本文详细介绍了如何利用Dify工作流和飞书多维表格构建自动化简历处理系统,实现简历上传、AI智能解析与结构化数据入库的全流程自动化。通过整合大语言模型进行信息抽取,该系统能显著提升HR处理简历的效率与准确性,将HR从重复劳动中解放出来。

1a2s3d4f5g的博客 1038

Java全栈:电影数据检索与推荐系统实战

全文检索个性化推荐数据缓存容器化部署通过这个项目,我们不仅实践了Java全栈开发的各项技术,还接触了推荐系统的核心算法实现。用户历史行为数据物品特征数据上下文信息实时反馈算法的准确性推荐的多样性系统的实时性冷启动问题性能优化效果评估VIEW, // 浏览CLICK, // 点击SEARCH, // 搜索RATE, // 评分COMMENT, // 评论SHARE, // 分享WATCH_TIME, // 观看时长。

exlink2012的专栏 894

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

Z-Image Turbo随机性增强插件的下载教程及使用方法 z-image生成图像太相似怎么办 Z-Image Turbo 整合包下载

Z-Image Turbo 是出了名的“seed 不敏感”,非常难做多样化图像创作。ComfyUi-ConditioningNoiseInjection 插件通过在conditioning(提示词编码)阶段加入可控噪声,成功解决了 Turbo 随机性不足的问题。它不仅能让 Turbo 产生更丰富的构图差异,还能保持主题稳定,是目前我测试过最有效的 Turbo 随机性增强工具。

代码简单说 Vue、JAVA、PHP、Node.js 熟练运用,接口、架构、性能全搞定。 2019

Leetcode310. Minimum Height Trees最小高度树

对于一个具有树特征的无向图,我们可选择任何一个节点作为根。图因此可以成为树,在所有可能的树中,具有最小高度的树被称为最小高度树。给出这样的一个图,写出一个函数找到所有的最小高度树并返回他们的根节点。 格式 该图包含n个节点,标记为0到n - 1。给定数字n和一个无向边edges列表(每一个边都是一对标签)。 你可以假设没有重复的边会出现在edges中。由于所有的边都是无...

lMonster81 384

Devexpress16.2.6破解工具

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

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

[Swift]LeetCode310. 最小高度树 | Minimum Height Trees

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)➤GitHub地址:https://github.com/strengthen/LeetCode➤原文地址:https://www.cnblogs.com/streng...

weixin_30906425的博客 178

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

【LeetCode】310. Minimum Height Trees 最小高度树(Medium)(JAVA)

【LeetCode】310. Minimum Height Trees 最小高度树(Medium)(JAVA) 题目地址: https://leetcode.com/problems/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

qq_16927853的博客 287

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

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

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

ln_ydc的专栏 188

leetcode 310. Minimum Height Trees | 310. 最小高度树(图的邻接矩阵DFS / 拓扑排序)

题目 https://leetcode.com/problems/minimum-height-trees/ 题解 方法1:图的邻接矩阵 DFS(超时) 我一想,这不就是个图嘛,于是随手敲出一个 DFS,结果。。超时了。 class Vertex { Integer val; List<Integer> next; public Vertex() { next = new LinkedList<>(); } } class Sol

寒泉 5万+

[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_30898109的博客 130

【leetcode_310】【中等】minimum-height-trees / 最小高度树

拓扑排序 无向图 degree自减

过得精彩,无所谓好坏 843

leetcode 310. Minimum Height Trees 图的构建+最小高度树+DFS深度优先遍历+拓扑排序

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

JackZhangNJU的专栏 1174

LeetCode C++ 310. Minimum Height Trees【Graph/BFS/拓扑排序】中等

Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1], nums2 = [2,2] Output: [2] Example 2: Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4] Output: [9,4] Note: Each element in the result must be unique. The re

memcpy0的博客 632

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 进阶的专栏 4539

【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

aliceyangxi1987的博客 2057

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 147. Insertion Sort List (链表插入排序)
下一篇: leetcode 878. Nth Magical Number (第N个魔法数)
蓝羽飞鸟
博客等级 码龄8年 1万+粉丝 727原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值