[Leetcode] [JavaScript] Minimum Height Trees

Source: Internet
Author: User

Minimum Height Trees

For a undirected graph with tree characteristics, we can choose any node as the root. The result graph is then a rooted tree. Among all possible rooted trees, those with minimum height is called minimum height trees (mhts). Given such a graph, write a function to find all the mhts and return a list of their root labels.

Format
The graph contains n nodes which is labeled from 0 to n - 1 . You'll be given the number and n a list of undirected edges (each edge is a pair of labels).

You can assume that no duplicate edges would appear in edges . Since all edges was undirected, is the same as and thus would not [0, 1] [1, 0] appear together in edges .

Example 1:

Given n = 4 ,edges = [[1, 0], [1, 2], [1, 3]]

        0        |        1       /       2   3

Return[1]

Example 2:

Given n = 6 ,edges = [[0, 3], [1, 3], [2, 3], [4, 3], [5, 4]]

     0  1  2      \ |/        3        |        4        |        5

Return[3, 4]

https://leetcode.com/problems/minimum-height-trees/

Test instructions is the highest tree with any node as root.

Can be converted to the center of the longest path from the leaves to the leaves, the result may be only one or two.

Build the tree, BFS traversal, each round to remove all the leaf nodes, and finally left is the result.

Open a variable visited record the number of traversed points, if visited >= n-2 instructions to find the results.

1 /**2 * @param {number} n3 * @param {number[][]} edges4 * @return {number[]}5  */6 varFindminheighttrees =function(n, edges) {7     if(n = = 1)return[0];8     varresult = [], tree = {}, list = [], I, J, Curr, visited = 0;9      for(i = 0; i < edges.length; i++){TenCurr =Edges[i]; One         if(!tree[curr[0]]) tree[curr[0]] =NewNode (curr[0]); A         if(!tree[curr[1]]) tree[curr[1]] =NewNode (curr[1]); -Tree[curr[0]].neighbor.push (tree[curr[1]]); -Tree[curr[1]].neighbor.push (tree[curr[0]]); the     } -      for(Iinchtree) { -         if(Tree[i].neighbor.length = = 1){ - List.push (tree[i].val); +         } -     } + BFS (list); A      for(i = 0; i < list.length; i++){ at Result.push (List[i]); -     } -     returnresult; -      -     functionNode (val) { -          This. val =Val; in          This. Neighbor = []; -     } to     functionBFS (list) { +         varLen =list.length, top, Topneighbor; -         if(Visited >= n-2)return; the          while(len--){ *visited++; $top =Tree[list.shift ()];Panax NotoginsengTopneighbor = top.neighbor[0]; - Deletenode (Topneighbor.neighbor, top.val); the             if(topNeighbor.neighbor.length <= 1 && list.indexof (topneighbor.val) = = =-1){ + List.push (topneighbor.val); A             } the             DeleteTree[top.val]; +         } - BFS (list); $     } $     functionDeletenode (arr, Val) { -          for(vari = 0; i < arr.length; i++){ -             if(Arr[i].val = = =val) { theArr.splice (i,1); -                 return;Wuyi             } the         } -     } Wu};

[Leetcode] [JavaScript] Minimum Height Trees

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.