二叉树的递归及非递归遍历

#include <iostream>
#include <vector>
#include <stack>

using namespace std;

//         1
//          \
//           2
//          /
//         3

struct TreeNode {
	int val;
	TreeNode* left;
	TreeNode* right;

	TreeNode() :val(0), left(nullptr), right(nullptr) {}
	TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
	TreeNode(int x, TreeNode* L, TreeNode* R) : val(x), left(L), right(R) {}
};

//二叉树的前中后递归遍历
//前序遍历  1 2 3
void treePreorderTraversal(TreeNode * root, vector<int>& output) {
	if (root == nullptr)
		return;

	output.push_back(root->val);
	treePreorderTraversal(root->left, output);
	treePreorderTraversal(root->right, output);
}

//中序递归遍历 1 3 2
void treeInorderTraversal(TreeNode* root, vector<int>& output) {
	if (root == nullptr)
		return;

	treeInorderTraversal(root->left, output);
	output.push_back(root->val);
	treeInorderTraversal(root->right, output);
}

//后序递归遍历 3 2 1
void treePostTraversal(TreeNode* root, vector<int>& output) {
	if (nullptr == root) {
		return;
	}

	treePostTraversal(root->left, output);
	treePostTraversal(root->right, output);
	output.push_back(root->val);
}

//二叉树的前中后遍历的非递归遍历
//二叉树的前序非递归遍历
vector<int> treePreorderTraversal(TreeNode* root) {
	vector<int> result;
	if (root == nullptr) {
		return result;
	}

	stack<TreeNode*> st;
	st.push(root);

	while (!st.empty()) {
		TreeNode* temp = st.top();
		result.push_back(temp->val);
		st.pop();

		if (temp->right != nullptr) {
			st.push(temp->right);
		}

		if (temp->left != nullptr) {
			st.push(temp->left);
		}
	}
	return result;
}

//二叉树的中序非递归遍历
vector<int> treeInorderTraversal(TreeNode* root) {
	vector<int> result;
	if (nullptr == root) {
		return result;
	}

	stack<TreeNode*> st;
	TreeNode* cur = root;

	while (!st.empty() || cur != nullptr) {
		if (cur != nullptr) {
			st.push(cur);
			cur = cur->left;
		}
		else {
			cur = st.top();
			result.push_back(cur->val);
			st.pop();
			cur = cur->right;
		}
	}

	return result;
}

//二叉树的后序非递归遍历
vector<int> treePostTraversal(TreeNode* root) {
	vector<int> result;
	if (root == nullptr) {
		return result;
	}
	//前序遍历是中左右  --> 中右左  --逆序--> 左右中
	//后序遍历:右左中
	
	//后序遍历可以通过前序遍历转换而来
	stack<TreeNode*> st;
	st.push(root);

	while (!st.empty()) {
		TreeNode* temp = st.top();
		result.push_back(temp->val);
		st.pop();

		//和前序遍历不同的是,先把左树放入栈中,这样才能出现中右左的结果
		if (temp->left != nullptr) {
			st.push(temp->left);
		}

		if (temp->right != nullptr) {
			st.push(temp ->right);
		}
	}
	return result;
}



//         1
//          \
//           2
//          /
//         3
TreeNode* buildBinaryTree() {
	TreeNode* a1 = new TreeNode(1);
	TreeNode* a2 = new TreeNode(2);
	TreeNode* a3 = new TreeNode(3);

	a1->left = nullptr;
	a1->right = a2;
	a2->left = a3;
	a2->right = nullptr;
	a3->left = nullptr;
	a3->right = nullptr;

	return a1;
}

int printVector(vector<int> vt) {
	vector<int>::iterator it;
	for (it = vt.begin(); it != vt.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;

	return 0;
}

int test() {
	TreeNode* root = buildBinaryTree();
	vector<int> test1;
	vector<int> test2;
	vector<int> test3;

	treePreorderTraversal(root, test1);
	treeInorderTraversal(root, test2);
	treePostTraversal(root, test3);

	cout << "前序遍历:" << endl;
	printVector(test1);

	cout << "中序遍历:" << endl;
	printVector(test2);

	cout << "后序遍历:" << endl;
	printVector(test3);

	cout << endl << endl << endl;


	vector<int> test4 = treePreorderTraversal(root);
	vector<int> test5 = treeInorderTraversal(root);
	vector<int> test6 = treePostTraversal(root); 

	cout << "非递归前序遍历:" << endl;
	printVector(test4);

	cout << "非递归中序遍历:" << endl;
	printVector(test5);

	cout << "非递归后序遍历:" << endl;
	printVector(test6);

	return 0;
}

int main() {
	test();
	return 0;
}

/结果:
/************************
	前序遍历:
	1 2 3
	中序遍历:
	1 3 2
	后序遍历:
	3 2 1
	
	非递归前序遍历:
	1 2 3
	非递归中序遍历:
	1 3 2
	非递归后序遍历:
	1 2 3
*************************/在这里插入代码片
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值