Flatten Binary Tree to Linked List
TreeNode* flat(TreeNode *root)
{
if(!root)
return NULL;
TreeNode *tail1 = flat(root->left);
TreeNode *tail2 = flat(root->right);
if(tail1)
{
tail1->right = root->right;
tail1->left = NULL;
root->right = root->left;
root->left = NULL;
}
if(tail2) return tail2;
if(tail1) return tail1;
return root;
}
void flatten(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
flat(root);
}
浙公网安备 33010602011771号