题目描述:

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

For example, Given

1
2
3
4
5
6
    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

题目中其实并没有说清楚怎么flatten的, 但是观察给的例子后可以发现是把一个节点的左子树插到右子树之前, 原来的右子树放到左子树的最右端叶子节点的右子树位置.

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
/**
* 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 flatten(TreeNode* root) {
TreeNode *p = root;
while(p){
if(p->left){
TreeNode *right = p->right;
p->right = p->left;
p->left = nullptr;
leftNode(p)->right = right;
}
p = p->right;
}
}

TreeNode* leftNode(TreeNode *root){
if(!root) return nullptr;
TreeNode *p = root;
while(p->right){
p = p->right;
}
return p;
}
};