【数据结构】二叉树后序遍历(非递归)

后序遍历:左-右-根

  • 思路分析: 

后序遍历的顺序是:左-右-根,和前、中序遍历的不同之处在于,它在遍历的时候,需要判断它的右子树是否已经被访问过,如果已经访问过的话,就直接取出当前节点,若没有访问过,则就去访问。如果没有这个操作,依旧和前、中序遍历的思想一样,就会在底部递归时,出现死循环。

  • 具体步骤如下:

①先将根节点入栈; 
②若根节点不存在左子树和右子树,或者根节点存在左子树或右子树,但左右子树已经被输出,则可以直接取出根节点,之后将其出栈,并将出栈节点标记为上一个输出的节点prev,再将此时的栈顶结点设为当前节点; 
③若不满足②中的条件,则将根节点的左子树和右子树依次入栈,当前节点重新置为栈顶结点,之后重复操作②,直到栈为空时,遍历结束。 

  • 具体实现代码如下:

void BinaryTreePostOrderNonR(BTNode* root)//后序遍历的非递归
{
	Stack st;
	StackInit(&st,10);
	BTNode* cur = root;
	BTNode* prev = NULL;
	while (cur || StackEmpty(&st) != 0)
	{
		//1.把压左路节点入栈
		while (cur)
		{
			StackPush(&st, cur);
			cur = cur->_left;
		}
		BTNode* top = StackTop(&st);
		//右树访问过了,才能访问根节点
		if (top->_right == NULL || top->_right == prev)//判断右树是否已经访问过,如果不判断就会出现死循环
		{
			printf("%c ", top->_data);
			StackPop(&st);
			prev = top;
		}
		else
		{
			cur = top->_right;
		}
	 }
	printf("\n");
}


 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,以下是C++数据结构中树的二叉树的前序、中序、后序遍历的递归与非递归代码,包括输入与输出。 假设二叉树的节点定义如下: ``` struct Node { int val; Node* left; Node* right; Node(int x) : val(x), left(NULL), right(NULL) {} }; ``` 前序遍历: 递归版本: ``` void preorderTraversal(Node* root) { if (!root) return; cout << root->val << " "; preorderTraversal(root->left); preorderTraversal(root->right); } ``` 非递归版本: ``` void preorderTraversal(Node* root) { stack<Node*> s; s.push(root); while (!s.empty()) { Node* node = s.top(); s.pop(); if (!node) continue; cout << node->val << " "; s.push(node->right); s.push(node->left); } } ``` 中序遍历: 递归版本: ``` void inorderTraversal(Node* root) { if (!root) return; inorderTraversal(root->left); cout << root->val << " "; inorderTraversal(root->right); } ``` 非递归版本: ``` void inorderTraversal(Node* root) { stack<Node*> s; Node* node = root; while (node || !s.empty()) { while (node) { s.push(node); node = node->left; } node = s.top(); s.pop(); cout << node->val << " "; node = node->right; } } ``` 后序遍历: 递归版本: ``` void postorderTraversal(Node* root) { if (!root) return; postorderTraversal(root->left); postorderTraversal(root->right); cout << root->val << " "; } ``` 非递归版本: ``` void postorderTraversal(Node* root) { stack<Node*> s; Node* node = root; Node* last = NULL; while (node || !s.empty()) { while (node) { s.push(node); node = node->left; } node = s.top(); if (!node->right || node->right == last) { cout << node->val << " "; s.pop(); last = node; node = NULL; } else { node = node->right; } } } ``` 输入与输出: ``` int main() { Node* root = new Node(1); root->left = new Node(2); root->right = new Node(3); root->left->left = new Node(4); root->left->right = new Node(5); root->right->left = new Node(6); root->right->right = new Node(7); cout << "前序遍历(递归): "; preorderTraversal(root); cout << endl; cout << "前序遍历(非递归): "; preorderTraversal(root); cout << endl; cout << "中序遍历(递归): "; inorderTraversal(root); cout << endl; cout << "中序遍历(非递归): "; inorderTraversal(root); cout << endl; cout << "后序遍历(递归): "; postorderTraversal(root); cout << endl; cout << "后序遍历(非递归): "; postorderTraversal(root); cout << endl; return 0; } ``` 输出结果: ``` 前序遍历(递归): 1 2 4 5 3 6 7 前序遍历(非递归): 1 2 4 5 3 6 7 中序遍历(递归): 4 2 5 1 6 3 7 中序遍历(非递归): 4 2 5 1 6 3 7 后序遍历(递归): 4 5 2 6 7 3 1 后序遍历(非递归): 4 5 2 6 7 3 1 ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值