LeetCode 1245 Tree Diameter
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相关推荐
- [LeetCode] Binary Tree Level Order Traversal 二叉树层次遍历(DFS | BFS)
目录: 1.Binary Tree Level Order Traversal - 二叉树层次遍历 BFS 2.Binary Tree Level Order Traversal II - 二叉树层次 ...
- 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. ...
- LeetCode N-ary Tree Level Order Traversal(bfs)
问题:给出一个n叉树,输出层次遍历 思路:bfs 具体代码参考: https://github.com/wuli2496/OJ/tree/master/LeetCode/N-ary%20Tree%20 ...
- LeetCode Binary Tree Right Side View(搜索)
问题:给出一个二叉树,要求输出右视图 思路:因为要求输出右视图.可以考虑使用深度优先搜索或者 广度优先搜索. 使用深度优先搜索时,以非递归形式,将左右子树入栈,同时使用哈希表记录深度与对应右视图的值. ...
- LeetCode Binary Tree Preorder Traversal(二叉树的前序遍历)
问题:给出一个二叉树,输出前序遍历 思路: 自顶向下遍历过程中,将当前结点的值加入到list中,然后处理左.右子树 具体代码参考: https://github.com/wuli2496/OJ/tre ...
- [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- [LeetCode] Binary Tree Postorder题解
Binary Tree Postorder Given a binary tree, return the postorder traversal of its nodes' values. For ...
- [Leetcode] Binary Tree Maximum Path Sum
这是LeetCode上的一道题目,需要求二叉树中两点路径的最大和.原题是 https://oj.leetcode.com/problems/binary-tree-maximum-path-sum/ ...
- [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 ...
最新文章
- Android之系统自带的文字外观设置
- 上海网络推广浅析外链对网站优化的影响有多大?需要注意什么?
- PCANet --- 用于图像分类的深度学习基准
- jstl 处理Date 时间
- [转载] C#面向对象设计模式纵横谈——16 Interpreter解释器模式
- linux下使用vi操作
- 读《python核心编程2》笔记 1
- ubuntu增加磁盘容量
- 如何获得鼠标选中的值
- Could not find a declaration file for module 'vue-xxx'.
- 【MATLAB】几种特殊矩阵,Hilbert矩阵,Toeplitz矩阵,Vandermonde矩阵......
- Win10桌面极简美化
- windows pe安装系统
- 零基础学HTML5的学习路线完整版
- 央视点赞!实景三维校园,最暖心的毕业礼...
- 解决win11/win10在登陆界面(解锁界面)点击获取每日壁纸无效的问题 - get Daily Lockscreen and Wallpaper - Win11/10的登录界面背景图片在哪里?
- Neural Ordinary Differential Equations
- sincerit 算法竞赛宝典--油桶问题
- 做题总结——王母娘娘又双叒叕来难为茶山牛了
- 如何用PHP实现上传图片功能