二叉树的三种遍历方式:递归、栈、循环

本文介绍了二叉树的三种遍历方法,包括递归、栈辅助和循环实现。递归方法直观但可能导致栈溢出,栈方法需要额外空间,循环实现则相对复杂。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

          三种方法中,递归最为简单,栈次之,循环最为麻烦。递归的深度如果太大则会导致栈溢出;栈的方式需要额外的辅助空间;循环编程最麻烦。

          首先是递归:

//递归方法
void midPrint_r(TreeNode* root)
{//中序遍历
	if(root==NULL)
		return;
	if(root->left)
		midPrint_r(root->left);
	cout<<root->val<<"   ";
	if(root->right)
		midPrint_r(root->right);
}

void prePrint_r(TreeNode* root)
{//前序遍历
	if(root==NULL)
		return;
	cout<<root->val<<"   ";
	if(root->left)
		prePrint_r(root->left);	
	if(root->right)
		prePrint_r(root->right);
}

void postPrint_r(TreeNode* root)
{//中序遍历
	if(root==NULL)
		return;
	if(root->left)
		postPrint_r(root->left);	
	if(root->right)
		postPrint_r(root->right);
	cout<<root->val<<"   ";
}

    栈方法:先循环把结点压入到栈中,然后再逐个弹出;

//循环堆栈方法
void midPrint_l(TreeNode* root)
{
	stack<TreeNode*> s;
	TreeNode *cur = root;
	while(!s.empty() || cur != NULL)
	{
		while(cur != NULL)
		{
			s.push(cur);
			cur = cur->left;
		}
		cur = s.top();
		s.pop();
		cout<<cur->val<<" ";
		cur = cur->right;
	}
}

void prePrint_l(TreeNode* root)
{
	stack<TreeNode*> s;
	TreeNode *cur = root;
	while(!s.empty() || cur != NULL)
	{
		while(cur != NULL)
		{
			cout<<cur->var<<" ";
			s.push(cur);
			cur = cur->left;
		}
		cur = s.top();
		s.pop();
		cur = cur->right;
	}
}

void postPrint_l(TreeNode* root)
{
	//the original method
	/*
	We use a prev variable to keep track of the previously-traversed node.
	Let’s assume curr is the current node that’s on top of the stack. When
	prev is curr‘s parent, we are traversing down the tree. In this case,
	we try to traverse to curr‘s left child if available (ie, push left
	child to the stack). If it is not available, we look at curr‘s right
	child. If both left and right child do not exist (ie, curr is a leaf node),
	we print curr‘s value and pop it off the stack.If prev is curr‘s left
	child, we are traversing up the tree from the left. We look at curr‘s
	right child. If it is available, then traverse down the right child (ie,
	push right child to the stack), otherwise print curr‘s value and pop it
	off the stack.If prev is curr‘s right child, we are traversing up the tree
	from the right. In this case, we print curr‘s value and pop it off the stack.
	*/
	if(root == NULL)
	{
		return;
	}
	stack<TreeNode*> s;
	s.push(root);
	TreeNode *cur = NULL;
	TreeNode *pre = NULL;
	while(!s.empty())
	{
		cur = s.top();
		//left down or right down
		if( pre == NULL || pre->left == cur || pre->right == cur)
		{//有左子树压左子树,有右子树压右子树,
		//都没有说明是叶子结点,直接打印并弹出
			if(cur->left != NULL)
			{//先压左子树,若左子树为空则压右子树
				s.push(cur->left);
			}
			else if(cur->right != NULL)
			{
				s.push(cur->right);
			}
			else
			{//左右子树均为空
				cout<<cur->var<<" ";
				s.pop();
			}
		}
		//left up
		else if(cur->left == pre)
		{//左边开始返回
			if(cur->right != NULL)
			{//若右孩子不为空则压入,否则,打印
				s.push(cur->right);
			}
			else
			{
				cout<<cur->var<<" ";
				s.pop();
			}
		}
		//right up
		else if(cur->right == pre)
		{//从右边返回,说明右侧已经打印完,则可以直接打印
			cout<<cur->var<<" ";
			s.pop();
		}
		pre = cur;
	}
	// the easy method
	/*stack<TreeNode*> s;
	s.push(root);
	TreeNode *cur = NULL;
	TreeNode *pre = NULL;
	while(!s.empty())
	{
	    cur = s.top();
	    if(pre == NULL || pre->left == cur || pre->right == cur)
		{
	        if(cur->left != NULL)
			{
	            s.push(cur->left);
	        }
	        else if(cur->right != NULL)
			{
	            s.push(cur->right);
	        }
	    }
	    else if(cur->left == pre)
		{
	        if(cur->right != NULL)
			{
	            s.push(cur->right);
	        }
	    }
	    else
		{
	        cout<<cur->var<<" ";
	        s.pop();
	    }
	    pre = cur;
	}*/
}

循环:利用叶子结点左右指针为空的特点,给叶子结点设置其直接后继,输出完该子结点后,再返回其直接后继;

//不用辅助空间的方式
void midPrint_m(TreeNode *root)
{
	TreeNode *cur = root;
	TreeNode *pre = NULL;
	while(cur != NULL)
	{
		if(cur->left == NULL)
		{//左孩子为空,则直接输出
			cout<<cur->var<<" ";
			cur = cur->right;
		}
		else
		{
			pre = cur->left;
			while( pre->right != NULL && pre->right != cur )
			{//找到cur的直接前驱
				pre = pre->right;
			}
			if(pre->right == NULL)
			{//设置后继结点
				pre->right = cur;
				cur = cur->left;
			}
			else
			{
				pre->right = NULL;//重新设置为空
				cout<<cur->var<<" ";
				cur = cur->right;
			}
		}
	}
}

void prePrint_m(TreeNode *root)
{//基本同于中遍历
	TreeNode *cur = root;
	TreeNode *pre = NULL;
	while(cur != NULL)
	{
		if(cur->left == NULL)
		{
			cout<<cur->var<<" ";
			cur = cur->right;
		}
		else
		{
			pre = cur->left;
			while( pre->right != NULL && pre->right != cur )
			{
				pre = pre->right;
			}
			if(pre->right == NULL)
			{
				pre->right = cur;
				cout<<cur->var<<" ";
				cur = cur->left;
			}
			else
			{
				pre->right = NULL;
				cur = cur->right;
			}
		}
	}
}
//this is the most difficult algorithm
void reverse_out(TreeNode *from,TreeNode *to)
{
	//first reverse from->to
	//reverse
	TreeNode *cur = from;
	TreeNode *post = cur->right;
	while(cur != to)
	{
		TreeNode *tmp = post->right;
		post->right = cur;
		cur = post;
		post = tmp;
	}
	//already reverse,output list
	TreeNode *traversal = cur;
	while( cur != from )
	{
		cout<<cur->var<<" ";
		cur = cur->right;
	}
	cout<<cur->var<<" ";
	//reverse original
	cur = to;
	post = cur->right;
	while(cur != from)
	{
		TreeNode *tmp = post->right;
		post->right = cur;
		cur = post;
		post = tmp;
	}
	//restore to's right
	to->right = NULL;
}
void postPrint_m(TreeNode *root)
{
	TreeNode *newroot = new TreeNode(0);
	newroot->left = root;
	newroot->right = NULL;
	TreeNode *cur = newroot;
	TreeNode *pre = NULL;
	while(cur != NULL)
	{
		if(cur->left == NULL)
		{
			cur = cur->right;
		}
		else
		{
			pre = cur->left;
			while(pre->right != NULL && pre->right != cur)
			{
				pre = pre->right;
			}
			if(pre->right == NULL)
			{
				pre->right = cur;
				cur = cur->left;
			}
			else
			{
				pre->right = NULL;
				reverse_out(cur->left,pre);
				cur = cur->right;
			}
		}
	}
}

    可见,前序、中序编程都比较简单,一量涉及后序就会比较麻烦。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值