NetEase 2016 Internship Research Engineer 3 programming Questions-2

Source: Internet
Author: User

There is a binary tree, each point on the tree of the right value, the weights are different, please design an algorithm to calculate the maximum weight of the leaf node to the minimum weight of the leaf node distance. The distance between each edge of the binary tree is 1, and the distance that one node passes through the number of edges to the other node for the two nodes.

For the root node of the given binary tree, return the distance you are seeking.

import java.util.arraylist;  //import java.util.hashmap;  //import  java.util.iterator;  //import java.util.linkedlist;  //import java.util.list;   //import java.util.map; import java.util.*;/*public class treenode {     int val = 0;    treenode left = null ;     treenode right = null;    public treenode (int  val)  {        this.val = val;     }}*/public class tree {        public static  void main (String[] args)  {          //  TODO Auto-generated method stub           Tree tree = new tree ();           treenode root =  new treenode (5);           treenode left  = new treenode (4);          treenode  Right = new treenode (6);           root.left  = left;          root.right = right;           treenode r2 = new treenode (8);           right.right = r2;           Map<TreeNode, TreeNode> map=new HashMap<> ();                     system.out.println (Tree.getdis (root));                     //dfs          /* Tree.dfs (root, map, 1);          system.out.println ( Tree.dpest);          for (Int i=0;i<tree.leaves.size (); i++ ) {             tree.showpath (Tree.leaves.get ( i), map);         }*/                     //bfs           //tree.bfs (Root);      }         private void showpath (treenode node,map<treenode,  Treenode> map) {           while (Map.containskey (node)) {               system.out.print (node.val+ "<--");               node = map.get (node);           }           System.out.println (node.val);       }      public  int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;             public int getdis (TreeNode root)  {         // write code here         HashMap<Integer, Integer> map = new HashMap<> ();            helper (root, map);           //  showmap (map);           system.out.println ("min:"   + min +  ", Max"  + max);           //  root.left=null;          List<Integer>  Path1 = getpath (min, map, new arraylist<integer> ());           list<integer> path2 = getpath (max, map, new  ArrayList<Integer> ());             int  len1 = path1.size ();           int len2  = path2.size ();           int result =  0;          for  (int i = len1 - 1, j  = len2 - 1; i >= 0 && j >= 0; i--,  j--)  {              if  ( Path1.get (i)  == path2.get (j))  {                   result = i + j;               }           }          return result;     }            private list<integer > getpath (int i, map<integer, integer> map, list<integer>  List)  {          while  (Map.containskey (i))  {               list.add (i);               i = map.get (i);           }          list.add (i);           return list;      }         void showmap (Map<integer, integer> map)  {           Iterator<Integer> iterator =  Map.keyset (). Iterator ();          while  ( Iterator.hasnext ())  {               Integer i = iterator.Next ();               system.out.println ( "Key:"  + i +  ", Value:"  + map.get (i));           }      }        int  dpest = 1;      List<TreeNode> leaves=new  Arraylist<> ();                 public void dfs (Treenode root, map<treenode, treenode> map, int &NBSP;DP)  {          if  (root == null)               return;           if  (root.left == null && root.right  == null) &nbsP {              //map.put (root, null );               dpest = dpest  < dp ? dp : dpest;               leaves.add (root);           }           if  (root.left != null)  {               map.put (Root.left, root);               dfs (Root.left, map, &NBSP;DP&NBSP;+&NBSP;1);          }           if  (root.right != null)  {         &Nbsp;     map.put (root.right, root);               dfs (root.right, map, dp + 1);           }      }      &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;PUBLIC&NBSP;VOID&NBSP;BFS (treenode root) {           LinkedList<TreeNode> queue = new  Linkedlist<> ();           queue.add (Root);           while (!queue.isempty ()) {                            treenode tmp=queue.poll ();//queue remove              &nbsp System.out.print (tmp.val+ " ");               //while ()               if ( Tmp.left!=null) {                   queue.add (Tmp.left);//queue add               }               if (tmp.right!=null) {                   queue.add (tmp.right);               }          }       }        public void helper (TreeNode root,  hashmap<integeR, integer> map)  {          if  (root  == null)  {               return;          }           if  (Root.left == null && root.right == null)  {              int val =  root.val;              min =  val < min ? val : min;               max = val > max ? val : max;           }           if  (ROot.left != null)  {               map.put (Root.left.val, root.val);               helper (Root.left, map);           }          if  (Root.right != null)  {              map.put ( Root.right.val, root.val);               helper (Root.right, map);          }       }      }


NetEase 2016 Internship Research Engineer 3 programming Questions-2

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.