标签:
http://www.acmerblog.com/inorder-tree-traversal-without-recursion-and-without-stack-5988.html
用叶子节点的空指针来记录当前节点的位置,然后一旦遍历到了叶子节点,发现叶子节点的右指针指向的是当前节点,那么就认为以当前节点的左子树已经遍历完成。
以inorder为例,初始化当前节点为root,它的遍历规则如下:
#include<stdio.h> #include<stdlib.h> struct tNode { int data; struct tNode* left; struct tNode* right; }; 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 { /* 找到current的前驱节点 */ pre = current->left; while(pre->right != NULL && pre->right != current) pre = pre->right; /* 将current节点作为其前驱节点的右孩子 */ if(pre->right == NULL) { pre->right = current; current = current->left; } /* 恢复树的原有结构,更改right 指针 */ 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 */ } struct tNode* newtNode(int data) { struct tNode* tNode = (struct tNode*) malloc(sizeof(struct tNode)); tNode->data = data; tNode->left = NULL; tNode->right = NULL; return(tNode); } /* 测试*/ int main() { /* 构建树结构如下: 1 / 2 3 / 4 5 */ struct tNode *root = newtNode(1); root->left = newtNode(2); root->right = newtNode(3); root->left->left = newtNode(4); root->left->right = newtNode(5); MorrisTraversal(root); return 0; }
标签:
原文地址:http://www.cnblogs.com/argenbarbie/p/5408438.html