给定一个二叉树,原地将它展开为链表。
例如,给定二叉树
1 / 2 5 / 3 4 6
将其展开为:
1 2 3 4 5 6
class Solution {
public:
TreeNode *last = NULL;
void flatten(TreeNode* root)
{
if(root == NULL)
return;
TreeNode *r = root ->right;
if(last != NULL)
{
last ->right = root;
}
last = root;
flatten(root ->left);
flatten(r);
root ->left = NULL;
}
};