-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path57.cpp
More file actions
53 lines (46 loc) · 1.1 KB
/
57.cpp
File metadata and controls
53 lines (46 loc) · 1.1 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
using namespace std;
struct TreeLinkNode {
int val;
struct TreeLinkNode *left;
struct TreeLinkNode *right;
struct TreeLinkNode *next;
TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
TreeLinkNode() {}
};
TreeLinkNode *newTree() {
TreeLinkNode *node = new TreeLinkNode;
int x;
cin >> x;
if (!x)node = NULL;
else {
node->val = x;
node->left->next = node;
node->left = newTree();
node->right->next = node;
node->right = newTree();
}
return node;
}
TreeLinkNode *GetNext(TreeLinkNode *pNode) {
if (!pNode)return NULL;
if (pNode->right) {
pNode = pNode->right;
while (pNode->left)pNode = pNode->left;
return pNode;
}
while (pNode->next) {
if (pNode->next->left == pNode)
return pNode->next;
pNode = pNode->next;
}
return NULL;
}
int main() {
ios::sync_with_stdio(false);
TreeLinkNode *node = newTree();
TreeLinkNode *pNode;
TreeLinkNode *res = GetNext(pNode);
cout << res->val;
return 0;
}