Given a binary tree, flatten it to a linked list in-place.

For example,
Given

1
2
3
4
5
    1
/ \
2 5
/ \ \
3 4 6

The flattened tree should look like:

1
2
3
4
5
6
7
8
9
10
11
1
\
2
\
3
\
4
\
5
\
6

二叉树转链表

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
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:

void helper(TreeNode* &root, TreeNode* &last) {
last = root;
if (root == nullptr)
return ;

TreeNode *mylast, *temp = root->right;
helper(root->left, mylast);
if (mylast) {
last = mylast;
last->right = root->right;
root->right = root->left;
root->left = nullptr;
}

helper(root->right, mylast);
if (mylast)
last = mylast;

}

void flatten(TreeNode* root) {
TreeNode *last;
helper(root, last);
}
};