Friday 10 May 2013

Write a function that returns the length of the longest leaf-to-leaf path in a binary.


/*  
     * At first problem seems to compute height of right subtree of root node and
     * height of left subtree of root node, then add them to get maximum lenght path.
     * But Its more complicated than this. Consider the following tree:
     *        100
     *       /  \
     *      70   150
     *     / \
     *    50  80
     *   / \   \
     *  40  60  90
     *     / \   \
     *    55  65  95
     *
     *  In this case, the root node is not even in the longest path (65-60-50-70-80-90-95).
     *  So, what you must do is the following: For each node n you must compute the following:
     *  • compute longest path in left subtree starting with the root of the left subtree.
     *  • compute longest path in right subtree starting with the root of the right subtree .
     *  • compute the longest path in left subtree (not necessarily starting with the root of the left subtree).
     *  • compute the longest path in right subtree (not necessarily starting with the root of the right subtree).
     *
     *
     *  Structure of node is modified.
     *  Now node contains 2 more integers lDepth and rDepth.
     *  lDepth => maximum height of  left sub-tree of a node.
     *  rDepth => maximum height of right sub-tree of a node.
     *  1. Idea is to use inorder traversal algo to initialize lDepth and rDepth of each node in a bottom up manner.
     *  2. If a node has no left child then set it’s lDepth = 0.
     *  3. Recursively call initalizeLRDepth with left child as argument.
     *  4. When backtracking occurs i.e. if  leftChild of ptr is not null then:
     *        set lDepth of current node (ptr) to 1+ max (lDepth,rDepth) of left Child of ptr.
     *  5. Recursively call initializeLRDepth with right child as argument.
     *  6. When backtracking occurs i.e. if rightChild of ptr is not null then:
     *        set rDepth of current node (ptr) to 1 + max(lDepth,rDepth) of right child of ptr.
     *  7. Select node having maximum lDepth+rDepth.
*/




      class node
      {
            private:
           int data;
           node *left;
           node *right;
           int lDepth;
           int rDetph;
      }


      void initializeLRDEPTH(node *ptr)
      {
            if(ptr)
            {
                  if(! ptr->getLeftChild())
                        ptr->setLDEPTH(0);
                 
                  initializeLRDEPTH(ptr->getLeftChild());
                  if(ptr->getLeftChild())
                        ptr->setLDEPTH(max(ptr->getLeftChild()->getLDEPTH(),ptr->getLeftChild()->getRDEPTH())+1);
                 
                  if(! ptr->getRightChild())
                        ptr->setRDEPTH(0);
                 
                  initializeLRDEPTH(ptr->getRightChild());
                  if(ptr->getRightChild())
                        ptr->setRDEPTH(max(ptr->getRightChild()->getLDEPTH(),ptr->getRightChild()->getRDEPTH())+1);
                 
            }
      }

No comments:

Post a Comment