Write a function that will determine whether 2 binary search

Write a function that will determine whether 2 binary search trees are similar (same shape but not necessarily the same data.) The header should be Write a function that determines whether a list (STL List) is sorted in non-decreasing order. The header should be

Solution

Hi, Please find my implementation:

template<class Item>
bool similar(ninary_trr_node* root1, binary_tree_node* root2){
  
   // empty trees are similar
   if(root1==NULL && root2 == NULL)
       return true;

   // empty tree is not similar to a non-empty one
   if(root1 == NULL || root2 == NULL)
       return false;

   // otherwise check recursively
   return similar(root1->left, root2->left) &&
           similar(root1->right, root2->right);
}


9)
   template<class Item>
   bool is_sorted(const list<Item> &l){

       // if size is zero or 1, return true
       if(l.size() <= 1)
           return true;

       // getting iterator pointing to first element
       list<Item>::iterator it = l.begin();

       // iterating size-1 times
       for(int i=0; i<l.size()-1; i++){
           Item first = *it; // getting current element
           ++it; // forwarding iterator
           Item second = *it; // getting next element

           // if these two are not in increasing order, return false
           if(second < first)
               return false;
       }

       return true;
   }

 Write a function that will determine whether 2 binary search trees are similar (same shape but not necessarily the same data.) The header should be Write a fun

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site