Advertisement
Guest User

Untitled

a guest
Mar 30th, 2015
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | None | 0 0
  1. void flattenRec(BinaryTree* root, BinaryTree*& head, BinaryTree*& tail)
  2. {
  3.     if(!root) return;
  4.     BinaryTree* leftHead=NULL, leftTail=NULL;
  5.     if(root->left!=NULL){
  6.         flattenRec(root->left, leftHead, leftTail);
  7.         root->left=leftTail;
  8.         leftTail->right=root;
  9.     }
  10.    
  11.     BinaryTree* rightHead=NULL, rightTail=NULL;
  12.     if(root->right!=NULL){
  13.         flattenRec(root->right, rightHead, rightTail);
  14.         root->right=rightHead;
  15.         rigthHead->left=root;
  16.     }
  17.    
  18.     head=leftHead? leftHead : root;
  19.     tail=rightTail? rightTail : root;
  20. }
  21.  
  22. void flatten(BinaryTree* root)
  23. {
  24.     BinaryTree* head;
  25.     BinaryTree* tail;
  26.    
  27.     flattenRec(root, head, tail);
  28.    
  29.     head->left=tail;
  30.     tail->right=head;    
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement