Write an algorithm to find the ‘next’ node (i.e., in-order successor) of a given node in a binary search tree where each node has a link to its parent.

 1 typedef struct Node
 2 {
 3     int val;
 4     Node *left,*right,*parent;
 5 }Node;
 6 
 7 Node* GetSuccessor(Node* node)
 8 {
 9     if(node->right)
10     {    Node *t = node->right;
11         while(t&&t->left)
12             t = t->left;
13         return t;
14     }
15     else
16     {
17         Node* p ;
18         while(p= node->parent)
19         {    if(p->left == node)
20                 break;
21             node = p;
22         }
23         return p;
24     }
25 }

 

 posted on 2013-08-07 16:09  xuanxu  阅读(146)  评论(0编辑  收藏  举报