Advertisement
Guest User

Untitled

a guest
Mar 29th, 2015
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.54 KB | None | 0 0
  1. BinaryTree* successor(BinaryTree* root, BinaryTree* node)
  2. {
  3.     if(!root || !node) return NULL;
  4.    
  5.     if(node->right){
  6.         BinaryTree* left=node->right;
  7.         while(left->left){
  8.             left=left->left;
  9.         }
  10.         return left;
  11.     }
  12.    
  13.     BinaryTree* succ=NULL;
  14.     while(root){
  15.         if(root==node){
  16.             return succ;
  17.         } else if(root->val>node->val) {
  18.             succ=root;
  19.             root=root->left;
  20.         } else {
  21.             root=root->right;
  22.         }
  23.     }
  24.     return NULL;
  25.    
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement