Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.54 KB | None | 0 0
  1. class _3A
  2. {
  3.     /* Helper function that allocates a new node with the
  4.     given data and null left and right pointers. */
  5.     static Node getNode(int data)
  6.     {
  7.         Node newNode = new Node();
  8.         newNode.data = data;
  9.         newNode.children = new ArrayList<>();
  10.         return newNode;
  11.     }
  12.  
  13.     // Function to check if there is a path from root
  14.     // to the given node. It also populates  
  15.     // 'arr' with the given path  
  16.     static boolean getPath(Node root, List<Node> arr, int x)
  17.     {
  18.         // if root is null
  19.         // there is no path
  20.         if (root==null)
  21.             return false;
  22.  
  23.         // push the node's value in 'arr'
  24.         arr.add(root);
  25.  
  26.         // if it is the required node
  27.         // return true
  28.         if (root.data == x)
  29.             return true;
  30.  
  31.         // else check whether the required node lies
  32.         // in the left subtree or right subtree of
  33.         // the current node
  34.         boolean checkFlag = false;
  35.         for (Node child : root.children)
  36.             checkFlag = checkFlag || getPath(child, arr, x);
  37.         if (checkFlag) return true;
  38.  
  39.         // required node does not lie either in the
  40.         // left or right subtree of the current node
  41.         // Thus, remove current node's value from
  42.         // 'arr'and then return false
  43.         arr.remove(arr.size()-1);
  44.         return false;
  45.     }
  46.  
  47.     // any two nodes in a tree  
  48.     static List<Node> pathBetweenNodes(Node root, int n1, int n2)
  49.     {
  50.         // List to store the path of
  51.         // first node n1 from root
  52.         List<Node> path1= new ArrayList<>();
  53.  
  54.         // List to store the path of
  55.         // second node n2 from root
  56.         List<Node> path2=new ArrayList<>();
  57.  
  58.         getPath(root, path1, n1);
  59.         getPath(root, path2, n2);
  60.  
  61.         int intersection = -1;
  62.  
  63.         // Get intersection point
  64.         int i = 0, j = 0;
  65.         while (i != path1.size() || j != path2.size()) {
  66.             // Keep moving forward until no intersection
  67.             // is found
  68.             if (i == j && path1.get(i) == path2.get(i)) {
  69.                 i++;
  70.                 j++;
  71.             }
  72.             else {
  73.                 intersection = j - 1;
  74.                 break;
  75.             }
  76.         }
  77.  
  78.         List<Node> path = new ArrayList<>();
  79.         // get general path between nodes
  80.         for (i = path1.size() - 1; i > intersection; i--)
  81.             path.add(path1.get(i));
  82.  
  83.         for (i = intersection; i < path2.size(); i++)
  84.             path.add(path2.get(i));
  85.         return path;
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement