C***U 发帖数: 2406 | 1 void MorrisTraversal(struct tNode *root)
{
struct tNode *current,*pre;
if(root == NULL)
return;
current = root;
while(current != NULL)
{
if(current->left == NULL)
{
printf(" %d ", current->data);
current = current->right;
}
else
{
/* Find the inorder predecessor of current */
pre = current->left;
while(pre->right != NULL && pre->right != current)
pre = pre->right;
/* Make current as right child of its inorder predecessor */
if(pre->right == NULL)
{
pre->right = current;
current = current->left;
}
// MAGIC OF RESTORING the Tree happens here:
/* Revert the changes made in if part to restore the original
tree i.e., fix the right child of predecssor */
else
{
pre->right = NULL;
printf(" %d ",current->data);
current = current->right;
} /* End of if condition pre->right == NULL */
} /* End of if condition current->left == NULL*/
} /* End of while */
} | h****n 发帖数: 1093 | 2 mark,回头好好研究一下
【在 C***U 的大作中提到】 : void MorrisTraversal(struct tNode *root) : { : struct tNode *current,*pre; : if(root == NULL) : return; : current = root; : while(current != NULL) : { : if(current->left == NULL) : {
| p*****2 发帖数: 21240 | | C***U 发帖数: 2406 | 4 不记得了
就看到有人发
【在 p*****2 的大作中提到】 : 这是啥公司的题呀?
| q****m 发帖数: 177 | 5 nice
【在 C***U 的大作中提到】 : void MorrisTraversal(struct tNode *root) : { : struct tNode *current,*pre; : if(root == NULL) : return; : current = root; : while(current != NULL) : { : if(current->left == NULL) : {
| c*****a 发帖数: 808 | | p*****2 发帖数: 21240 | | l*****a 发帖数: 559 | |
|