Given a binary tree, flatten it to a linked list in-place.
For example,
Given
1
/ \
2 5
/ \ \
3 4 6
The flattened tree should look like:
1
\
2
\
3
\
4
\
5
\
6
class Solution {
public:
void flatten(TreeNode* root) {
if(!root) return ;
if(root->left) //不要漏
{flatten(root->left);
TreeNode *p=root->left,*q=root->left;
while(p->right) p=p->right;
p->right=root->right;
root->right=q;
root->left=NULL;}
flatten(root->right);//不要漏
}
};