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);
            }
        }
}
相关推荐
Stardep18 分钟前
算法学习11——滑动窗口——最大连续1的个数
数据结构·c++·学习·算法·leetcode·动态规划·牛客网
双叶83625 分钟前
(C语言)学生信息表(学生管理系统)(基于通讯录改版)(正式版)(C语言项目)
c语言·开发语言·c++·算法·microsoft
AQin101231 分钟前
【Leetcode·中等】如何初始化(583.两个字符串的删除操作·Delete Operation for Two Strings)
java·算法·leetcode·动态规划
green5+132 分钟前
LeetCode1两数之和
算法·哈希算法·散列表
hnsqls35 分钟前
LeetCode 精简75 题
算法·leetcode·职场和发展
石去皿38 分钟前
力扣hot100 31-40记录
java·算法·leetcode
不吃元西1 小时前
leetcode 118. 杨辉三角
算法·leetcode·职场和发展
wen__xvn1 小时前
每日一题洛谷P8716 [蓝桥杯 2020 省 AB2] 回文日期c++
c++·算法·蓝桥杯
梭七y1 小时前
【力扣hot100题】(027)两数相加
算法·leetcode·职场和发展
破东风1 小时前
leetcode每日一题:向字符串添加空格
算法·leetcode·模拟