Lintcode 3686 · N 叉树的直径【中等 DFS/BFS java答案】

题目


题目链接:https://www.lintcode.com/problem/3686/

思路

复制代码
1.利用map创建图
2.找到直径的其中一个端点last,通过bfs可以实现
3.从last出发,再次bfs,有多少层,直径就是多少

Java代码

java 复制代码
/**
 * Definition for Undirected graph.
 * class UndirectedGraphNode {
 *     int label;
 *     List<UndirectedGraphNode> neighbors;
 *     UndirectedGraphNode(int x) {
 *         label = x;
 *         neighbors = new ArrayList<UndirectedGraphNode>();
 *     }
 * }
 */

public class Solution {
    /**
     * @param root: the root of the tree
     * @return: Maximum diameter of the N-ary Tree
     */
    public int diameter(UndirectedGraphNode root) {
        if(root ==null) return 0;
         Map<UndirectedGraphNode, Set<UndirectedGraphNode>> graph = new HashMap<>();
            buildGraph(root, graph);

            //宽度右边遍历2次,第一次最后遍历到的节点last,
            // last一定是直径的一个端点之一
            // 那么从last出发,再次宽度优先遍历,有几层,直径就是几
            Set<UndirectedGraphNode> visited = new HashSet<>();
            UndirectedGraphNode last = null;
            Queue<UndirectedGraphNode> q = new LinkedList<>();
            q.add(root);
            visited.add(root);
            while (!q.isEmpty()){
                int size = q.size();
                for (int i = 0; i <size ; i++) {
                    UndirectedGraphNode pop = q.poll();
                    Set<UndirectedGraphNode> nexts = graph.get(pop);
                    for (UndirectedGraphNode next : nexts) {
                        if(!visited.contains(next)){
                            q.add(next);
                            last = next;
                             visited.add(next);
                        }
                    }
                }
            }

             //从last出发再次BFS
            int ans =0;
            visited.clear();
            q.add(last);
            visited.add(last);
            while (!q.isEmpty()){
                ans++;
                int size = q.size();
                for (int i = 0; i <size ; i++) {
                    UndirectedGraphNode pop = q.poll();
                    Set<UndirectedGraphNode> nexts = graph.get(pop);
                    for (UndirectedGraphNode next : nexts) {
                        if(!visited.contains(next)){
                            q.add(next);
                            visited.add(next);
                        }
                    }
                }
            }

            return ans-1;
        }

        public void buildGraph(UndirectedGraphNode cur, Map<UndirectedGraphNode, Set<UndirectedGraphNode>> g) {
            //建立图
            if (cur == null) return;
            if (!g.containsKey(cur)) {
                g.put(cur, new HashSet<>());
            }

            for (UndirectedGraphNode next : cur.neighbors) {
                if (!g.containsKey(next)) {
                    g.put(next, new HashSet<>());
                }

                g.get(cur).add(next);
                g.get(next).add(cur);
                buildGraph(next, g);
            }
        }
}
相关推荐
ai.Neo6 分钟前
牛客网NC22012:判断闰年问题详解
开发语言·c++·算法
好吃的肘子13 分钟前
ElasticSearch进阶
大数据·开发语言·分布式·算法·elasticsearch·kafka·jenkins
CIb0la28 分钟前
数据可视化
程序人生·算法·信息可视化
袁气满满~_~32 分钟前
LeetCode:617、合并二叉树
算法·leetcode·二叉树
写个博客33 分钟前
代码随想录算法训练营第四十一天
算法
像风一样自由202038 分钟前
算法模型部署后_python脚本API测试指南-记录3
python·算法·支持向量机
TO ENFJ1 小时前
day 17 无监督学习之聚类算法
学习·算法·聚类
GIS小天1 小时前
AI预测3D新模型百十个定位预测+胆码预测+去和尾2025年5月15日第78弹
人工智能·算法·机器学习·彩票
共享家95271 小时前
红黑树解析
数据结构·c++·算法
边跑边掩护1 小时前
LeetCode 820 单词的压缩编码题解
算法·leetcode·职场和发展