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);
            }
        }
}
相关推荐
wolf犭良1 小时前
37、aiomysql实操习题
开发语言·python·算法
xin007hoyo1 小时前
算法笔记.spfa算法(bellman-ford算法的改进)
数据结构·笔记·算法
向哆哆1 小时前
Java 加密与解密:从算法到应用的全面解析
java·开发语言·算法
uhakadotcom1 小时前
刚发布的PyTorch 2.7提供了什么 新特性
算法·面试·github
新生农民2 小时前
30分钟解决8道算法题
java·数据结构·算法
bbc1212262 小时前
2025/4/23 心得
数据结构·算法
WaitWaitWait017 小时前
LeetCode每日一题4.20
算法·leetcode
蒟蒻小袁7 小时前
力扣面试150题--有效的括号和简化路径
算法·leetcode·面试
跳跳糖炒酸奶8 小时前
第十五讲、Isaaclab中在机器人上添加传感器
人工智能·python·算法·ubuntu·机器人
明月看潮生9 小时前
青少年编程与数学 02-018 C++数据结构与算法 06课题、树
数据结构·c++·算法·青少年编程·编程与数学