208 字
1 分钟
不用栈的二叉树中序遍历
不用栈的二叉树中序遍历
// 中序遍历非栈非递归void inOrder_noRecursion_noStack(TreeNode* r, char* pattern){ TreeNode* current = r; while(current) { if(current->lchild == NULL) { // 当前节点没有左孩子,所以是应该访问的结点 printf(pattern, current->value); current = current->rchild; } else { TreeNode* past = current->lchild; // past一直到最右下 while(past->rchild != NULL && past->rchild != current) past = past->rchild;
if(past->rchild == NULL) { // 此时current是past的后继结点,建立链接方便下次找到后继节点 past->rchild = current; current = current->lchild; } else { /* 此时past的右孩子指向后继节点,所以是第二次访问 代表当前结点的左子树已经访问完毕,所以访问当前结点 然后把指针移到后继或者右子树,并消除多余链接 */ printf(pattern, current->value); past->rchild = NULL; current = current->rchild; } } }}原文链接:CSDN
不用栈的二叉树中序遍历
https://blog.homura.work/posts/csdn/109318879/