zoukankan      html  css  js  c++  java
  • LeetCode 1245. Tree Diameter

    原题链接在这里:https://leetcode.com/problems/tree-diameter/

    题目:

    Given an undirected tree, return its diameter: the number of edges in a longest path in that tree.

    The tree is given as an array of edges where edges[i] = [u, v] is a bidirectional edge between nodes u and v.  Each node has labels in the set {0, 1, ..., edges.length}.

    Example 1:

    Input: edges = [[0,1],[0,2]]
    Output: 2
    Explanation: 
    A longest path of the tree is the path 1 - 0 - 2.
    

    Example 2:

    Input: edges = [[0,1],[1,2],[2,3],[1,4],[4,5]]
    Output: 4
    Explanation: 
    A longest path of the tree is the path 3 - 2 - 1 - 4 - 5.

    Constraints:

    • 0 <= edges.length < 10^4
    • edges[i][0] != edges[i][1]
    • 0 <= edges[i][j] <= edges.length
    • The given edges form an undirected tree.

    题解:

    If it is a tree, then its nodes n = edges.length + 1.

    Build the tree graph first. Then start DFS from node 0.

    DFS returns the deepest depth. DFS state needs current node, tree graph, and parent.

    For current node, for all its neighbors, as long as it is not parent, get the depth from it, pick 2 largest. 

    Use these 2 largest to update the diameter.

    And returns the largest depth + 1.

    Time Complexity: O(n). n = edges.length.

    Space: O(n).

    AC Java:

     1 class Solution {
     2     int res = 0;
     3     
     4     public int treeDiameter(int[][] edges) {
     5         if(edges == null || edges.length == 0){
     6             return 0;
     7         }
     8         
     9         int n = edges.length + 1;
    10         ArrayList<Integer>[] graph = new ArrayList[n];
    11         for(int i = 0; i < n; i++){
    12             graph[i] = new ArrayList<Integer>();
    13         }
    14         
    15         for(int [] e : edges){
    16             graph[e[0]].add(e[1]);
    17             graph[e[1]].add(e[0]);
    18         }
    19 
    20         dfs(0, graph, -1);
    21         
    22         return res;
    23     }
    24     
    25     private int dfs(int root, ArrayList<Integer>[] graph, int parent){        
    26         int max1 = 0;
    27         int max2 = 0;
    28         for(int next : graph[root]){
    29             if(next != parent){
    30                 int depth = dfs(next, graph, root);
    31                 if(depth > max1){
    32                     max2 = max1;
    33                     max1 = depth;
    34                 }else if(depth > max2){
    35                     max2 = depth;
    36                 }
    37             }
    38         }
    39         
    40         res = Math.max(res, max1 + max2);
    41         return max1 + 1;
    42     }
    43 }

    类似Diameter of Binary Tree.

  • 相关阅读:
    Java StringBuilder、基本类型的包装类
    立个Flag不学好PHP誓不罢休
    LAMP搭建 转
    CentOS使用yum源中自带的rpm包安装LAMP环境
    CentOS RedHat YUM 源扩展补充(32位、64位均有)
    解决phpmyadmin上传文件大小限制的配置方法
    lanmp v2.5一键安装包发布(包括lamp,lnmp,lnamp安装)
    图像处理 jpg png gif svg
    NAT模式/路由模式/全路由模式 (转)
    网页制作中绝对路径和相对路径的区别
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/12365031.html
Copyright © 2011-2022 走看看