Given an undirected tree, return it’s diameter, which is the number of edges in a longest path in that tree.

the input is int[][] edges, that means the tree has no roots and it’s represents in adjancent matrix.
but we can’t see the tree as graph because tree is graph without cycle, so a tree is a tree and a graph can be a tree but not necessarily, and the problem can be convert to the longest path in a tree(with the weight of each edge is 1)

class Solution {private int diameter = 0;public int treeDiameter(int[][] edges) {//convert int[][] to List<Integer>[]List<Integer>[] graph = new List[edges.length + 1]; //this is a arraylist with list in it, but why with length of edges.length + 1?for (int i = 0; i <= edges.length; i++) {graph[i] = new ArrayList<>();}for (int[] e: edges) {graph[e[0]].add(e[1]);graph[e[1]].add(e[0]);}depth(0, -1, graph); //root level, it's parent levelreturn diameter;}private int depth(int root, int parent, List<Integer>[] graph) { //the max depth of current node//the core idea of this function is: longest path through a node is sum of top 2 depth of children's treeint maxDepth1st = 0; //the largest int maxDepth2nd = 0; //the second largestfor (int child: graph[root]) { //for each child of current root(not root to be precise)if (child == parent) continue;  Only one way from root node to child node, don't allow child node go to root node again!int childDepth = depth(child, root, graph);//check whether we need to update the 1st largest and the second largestif (childDepth > maxDepth1st) { maxDepth2nd = maxDepth1st;maxDepth1st = childDepth;} else if (childDepth > maxDepth2nd) {maxDepth2nd = childDepth;}}int longestPath = maxDepth1st + maxDepth2nd + 1; //it counts the number of nodesdiameter = Math.max(diameter, longestPath - 1); //maintain a global maximalreturn maxDepth1st + 1;}
}

LeetCode 1245 Tree Diameter相关推荐

  1. [LeetCode] Binary Tree Level Order Traversal 二叉树层次遍历(DFS | BFS)

    目录: 1.Binary Tree Level Order Traversal - 二叉树层次遍历 BFS 2.Binary Tree Level Order Traversal II - 二叉树层次 ...

  2. Leetcode | Binary Tree Maximum Path Sum

    Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...

  3. LeetCode N-ary Tree Level Order Traversal(bfs)

    问题:给出一个n叉树,输出层次遍历 思路:bfs 具体代码参考: https://github.com/wuli2496/OJ/tree/master/LeetCode/N-ary%20Tree%20 ...

  4. LeetCode Binary Tree Right Side View(搜索)

    问题:给出一个二叉树,要求输出右视图 思路:因为要求输出右视图.可以考虑使用深度优先搜索或者 广度优先搜索. 使用深度优先搜索时,以非递归形式,将左右子树入栈,同时使用哈希表记录深度与对应右视图的值. ...

  5. LeetCode Binary Tree Preorder Traversal(二叉树的前序遍历)

    问题:给出一个二叉树,输出前序遍历 思路: 自顶向下遍历过程中,将当前结点的值加入到list中,然后处理左.右子树 具体代码参考: https://github.com/wuli2496/OJ/tre ...

  6. [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历

    Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...

  7. [LeetCode] Binary Tree Postorder题解

    Binary Tree Postorder Given a binary tree, return the postorder traversal of its nodes' values. For ...

  8. [Leetcode] Binary Tree Maximum Path Sum

    这是LeetCode上的一道题目,需要求二叉树中两点路径的最大和.原题是 https://oj.leetcode.com/problems/binary-tree-maximum-path-sum/ ...

  9. [LeetCode] N-ary Tree Postorder Traversal N叉树的后序遍历

    Given an n-ary tree, return the postorder traversal of its nodes' values. For example, given a 3-ary ...

最新文章

  1. Android之系统自带的文字外观设置
  2. 上海网络推广浅析外链对网站优化的影响有多大?需要注意什么?
  3. PCANet --- 用于图像分类的深度学习基准
  4. jstl 处理Date 时间
  5. [转载] C#面向对象设计模式纵横谈——16 Interpreter解释器模式
  6. linux下使用vi操作
  7. 读《python核心编程2》笔记 1
  8. ubuntu增加磁盘容量
  9. 如何获得鼠标选中的值
  10. Could not find a declaration file for module 'vue-xxx'.
  11. 【MATLAB】几种特殊矩阵,Hilbert矩阵,Toeplitz矩阵,Vandermonde矩阵......
  12. Win10桌面极简美化
  13. windows pe安装系统
  14. 零基础学HTML5的学习路线完整版
  15. 央视点赞!实景三维校园,最暖心的毕业礼...
  16. 解决win11/win10在登陆界面(解锁界面)点击获取每日壁纸无效的问题 - get Daily Lockscreen and Wallpaper - Win11/10的登录界面背景图片在哪里?
  17. Neural Ordinary Differential Equations
  18. sincerit 算法竞赛宝典--油桶问题
  19. 做题总结——王母娘娘又双叒叕来难为茶山牛了
  20. 如何用PHP实现上传图片功能

热门文章

  1. HttpClient进行服务器传递信息,HttpUtil工具类
  2. (未完)p2p金融项目+实名认证
  3. dlib 怎么安装vs2017_dlib开发环境部署
  4. 与Java相关的四十个名字-Java基础-Java-编程开发
  5. Mac下快捷键的符号所对应的按键
  6. Python实现自动小One易统计打卡
  7. 大数据美食——寻找地图上的美味
  8. mulesoft Module 8 quiz 解析
  9. 利用Matlab处理Lumerical FDTD的三角纳米片电场分布仿真结果
  10. 【待更新】GPU 保存模型参数,GPU 加载模型参数