-
Notifications
You must be signed in to change notification settings - Fork 0
/
20180211GetNext
39 lines (37 loc) · 958 Bytes
/
20180211GetNext
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
struct TreeLinkNode {
int val;
struct TreeLinkNode *left;
struct TreeLinkNode *right;
struct TreeLinkNode *next;
TreeLinkNode(int x) :val(x), left(NULL), right(NULL), next(NULL) {
}
};
*/
class Solution {
public:
TreeLinkNode* GetNext(TreeLinkNode* pNode)
{
if(pNode==nullptr)
return nullptr;
if(pNode->right!=nullptr){
TreeLinkNode* p=pNode->right;
while(p->left!=nullptr){
p=p->left;
}
return p;
}
if(pNode->next!=nullptr){
if(pNode==pNode->next->left)
return pNode->next;
if(pNode==pNode->next->right){
TreeLinkNode* p=pNode->next;
while(p->next!=nullptr&&p==p->next->right){
p=p->next;
}
return p->next;
}
}
return nullptr;
}
};