题目:link
/**
* 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) {
while( root != NULL ){
if( root->left != NULL ){
TreeNode* mostRchild = root->left;
while( mostRchild->right != NULL ) mostRchild = mostRchild->right;
mostRchild->right = root->right;
root->right = root->left;
root->left = NULL;
}
root = root->right;
}
return;
}
};