m**p 发帖数: 189 | 1 哪位大侠知道原因?
void flatten(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (root==NULL) return;
TreeNode* curr = root;
while (curr) {
if (curr->left) {
TreeNode* temp = curr->left;
while (temp->right) {
temp = temp->right;
}
temp->right = curr->right;
curr->right = curr->left;
}
curr=curr->right;
}
return;
} |
n******d 发帖数: 386 | 2 After you set curr->right = curr->left, you forgot to set curr->left to NULL
, so while loop becomes dead loop
【在 m**p 的大作中提到】 : 哪位大侠知道原因? : : void flatten(TreeNode *root) { : // Start typing your C/C++ solution below : // DO NOT write int main() function : if (root==NULL) return; : : TreeNode* curr = root; : while (curr) { : if (curr->left) {
|
m**p 发帖数: 189 | 3 Thanks! I see it became a loop. |