/**
* 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:
TreeNode* tmp = NULL;
void flatten(TreeNode* root) {
if (!root) {
return;
}
if (tmp) {
//將tmp的right child指向下個root
tmp->left = NULL;
tmp->right = root;
}
//將tmp指向下個root(移動)
tmp = root;
//儲存right
TreeNode *right = root->right;
//先整理left
flatten(root->left);
//整理right
flatten(right);
}
};