c++实现---二叉树先序、中序、后序遍历的递归与非递归实现以及层次遍历

二叉树先序、中序、后序遍历的递归与非递归实现以及层次遍历。

#include<iostream>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
vector<vector<int>> res;
vector<int> help;
 struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
	TreeNode(int value) :val(value), left(nullptr), right(nullptr){}
};
 void insertNode(TreeNode *root,int &value){//构建搜索二叉树,左子节点的值小于等于根节点的值,右子节点的值大于等于根节点的值
	 if (value<=root->val){//小了往左边插入
		 if (!root->left){
			 root->left = new TreeNode(value);
		 }
		 else{
			 insertNode(root->left,value);
		 }
	 }
	 else{//大了往右边插入
		 if (!root->right){
			 root->right = new TreeNode(value);
		 }
		 else{
			 insertNode(root->right, value);
		 }
	 }
 }

	void preOrder(TreeNode* root){//非递归实现先序遍历
		stack<TreeNode*> nstack;
		nstack.push(root);
		while (!nstack.empty()){
			TreeNode* tmp = nstack.top();
			help.push_back(tmp->val);
			nstack.pop();
			if (tmp->right){//栈是先进后出的,因此先入右节点,后入左节点。
				nstack.push(tmp->right);
			}
			if (tmp->left){
				nstack.push(tmp->left);
			}
		}
		res.push_back(help);
		help.clear();
	}
	void preOrder1(TreeNode* root){//递归实现先序遍历
		if (root){
			help.push_back(root->val);
			preOrder1(root->left);
			preOrder1(root->right);
		}
	}
	void midOrder(TreeNode* root){//非递归实现中序遍历
		stack<TreeNode*> nstack;
		TreeNode* tmp = root;
		while (tmp || !nstack.empty()){
			if (tmp){
				nstack.push(tmp);
				tmp = tmp->left;
			}
			else{
				tmp = nstack.top();
				help.push_back(tmp->val);
				nstack.pop();
				tmp = tmp->right;
			}
		}
		res.push_back(help);
		help.clear();
	}
	void midOrder1(TreeNode* root){//递归实现中序遍历
		if (root){
			midOrder1(root->left);
			help.push_back(root->val);
			midOrder1(root->right);
		}
	}
	void postOrder(TreeNode* root){//非递归实现后序遍历(特殊,两个栈实现)
		stack<TreeNode*> nstack1, nstack2;
		nstack1.push(root);
		while (!nstack1.empty()){
			TreeNode* tmp = nstack1.top();
			nstack1.pop();
			nstack2.push(tmp);//借助栈1实现栈2入栈顺序:根、右、左;最后出栈就是后序遍历顺序
			if (tmp->left){
				nstack1.push(tmp->left);
			}
			if (tmp->right){
				nstack1.push(tmp->right);
			}
		}
		while (!nstack2.empty()){
			TreeNode* tmp = nstack2.top();
			help.push_back(tmp->val);
			nstack2.pop();
		}
		res.push_back(help);
		help.clear();
	}
	void postOrder1(TreeNode* root){//递归实现后序遍历
		if (root){
			postOrder1(root->left);
			postOrder1(root->right);
			help.push_back(root->val);
		}
	}
	void bfsOrder(TreeNode* root){//层次遍历(使用队列实现--先进先出)
		queue<TreeNode*> nqueue;
		nqueue.push(root);
		while (!nqueue.empty()){
			TreeNode *tmp = nqueue.front();
			help.push_back(tmp->val);
			nqueue.pop();
			if (tmp->left){
				nqueue.push(tmp->left);
			}
			if (tmp->right){
				nqueue.push(tmp->right);
			}
		}
		res.push_back(help);
		help.clear();
	}
	vector<vector<int> > fourOrders(TreeNode* root) {
		if (root == nullptr)
			return res;
		preOrder1(root);
		midOrder1(root);
		postOrder1(root);
		bfsOrder(root);
		return res;
	}

int main(){
	int n;
	while (cin >> n){
		n--;
		int value;
		cin >> value;
		TreeNode root(value);
		while (n--){
			int newValue;
			std::cin >> newValue;
			insertNode(&root, newValue);
		}
		fourOrders(&root);
		for (int i = 0; i < res.size();++i){
			for (int j = 0; j < res[j].size(); ++j){
				cout << res[i][j];
			}
			cout << endl;
		}
	}
	return 0;
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值