-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1002.cc
41 lines (37 loc) · 895 Bytes
/
1002.cc
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
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(nullptr), right(nullptr) {
}
};
class Solution {
public:
TreeNode* Convert(TreeNode* pRootOfTree)
{
if (pRootOfTree == nullptr) {
return nullptr;
}
TreeNode *tail;
return Convert(pRootOfTree, tail);
}
TreeNode* Convert(TreeNode* root, TreeNode* &tail) {
TreeNode* head;
if (root->left == nullptr) {
head = root;
} else {
head = Convert(root->left, tail);
tail->right = root;
root->left = tail;
}
if (root->right == nullptr) {
tail = root;
} else {
TreeNode* rHead = Convert(root->right, tail);
root->right = rHead;
rHead->left = root;
}
return head;
}
};