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);
            }
        }
}
相关推荐
ZhouDevin18 小时前
算法论文/模型微调2——仅骨干1%~3% 参数达到与全量微调相当的性能
算法
Sinosecu-OCR19 小时前
文通OCR技术深度解析:自研算法如何搞定复杂场景识别?
算法·ocr·ocr识别系统
CQU_JIAKE19 小时前
8.5【A】
数据结构·算法
ESBK202519 小时前
学术邀约|CMICA 2026 第二届计算方法、智能控制与航空航天国际会议
大数据·人工智能·算法·飞行器·智能控制·航空航天·计算
无bug代码搬运工19 小时前
LeetCode 42 接雨水:双指针解法详解
算法·leetcode·职场和发展
Jasmine_llq20 小时前
《P13016 [GESP202506 六级] 最大因数》
数据结构·算法
白狐_79820 小时前
考研408算法设计题保命策略:链表专题暴力解法精讲(2015 & 2019真题实战)
考研·算法·链表
fangpin20 小时前
GPU编程2: 并行策略
算法
碧海银沙音频科技研究院20 小时前
8基于BES2700IHC数字助听器系统开发
嵌入式硬件·算法
HZZD_HZZD21 小时前
智能电表选CoAP还是MQTT?合众致达万表实测:CoAP省流17.3%、续航提升38%,附报文拆解与选型矩阵
算法