【second】Flatten Binary Tree to Linked List
递归
void flatten(TreeNode *root) { // Note: The Solution object is instantiated only once and is reused by each test case. flat(root); } TreeNode* flat(TreeNode* root) { if(!root) return NULL; TreeNode* left_tail = flat(root->left); TreeNode* right_tail = flat(root->right); if(left_tail) { left_tail->right = root->right; root->right = root->left; root->left = NULL; } if(right_tail) return right_tail; if(left_tail) return left_tail; return root; }