标签:
中序遍历二叉树,递归展开。
别忘了将left设为NULL,如果忘掉的话可能报Runtime Error,而且这个RE很难查出原因。
代码:
1 TreeNode *solve(TreeNode *root) { 2 if (!root) return NULL; 3 4 TreeNode *leftHead = root->left; 5 TreeNode *leftTail = solve(leftHead); 6 TreeNode *rightHead = root->right; 7 TreeNode *rightTail = solve(rightHead); 8 TreeNode *tail = root; 9 10 // 这一句别忘了 11 tail->left = tail->right = NULL; 12 13 if (leftHead) { 14 tail->right = leftHead; 15 tail = leftTail; 16 } 17 if (rightHead) { 18 tail->right = rightHead; 19 tail = rightTail; 20 } 21 22 return tail; 23 } 24 25 void flatten(TreeNode *root) { 26 solve(root); 27 }
Leetcode#114 Flatten Binary Tree to Linked List
标签:
原文地址:http://www.cnblogs.com/boring09/p/4261761.html