二叉树的前、中、后序遍历【c++】

前序遍历:根左右
中序遍历:左根右
后序遍历:左右根

#include <iostream>
#include <vector>
using namespace std;

//双链表节点结构
typedef struct treeNode {
	int value;
	struct treeNode* left;
	struct treeNode* right;
	treeNode(int x) : value(x), left(nullptr), right(nullptr) {}
} Node;

//前序遍历
void preOrder(Node* root, vector<int> &allval)
{
	if (!root)
	{
		return;
	}
	allval.push_back(root->value);  //遍历根节点
	preOrder(root->left, allval);   //遍历左节点
	preOrder(root->right, allval);  //遍历右节点
}

//中序遍历
void inOrder(Node* root, vector<int> &allval)
{
	if (!root)
	{
		return;
	}
	inOrder(root->left, allval);   //遍历左节点
	allval.push_back(root->value);  //遍历根节点
	inOrder(root->right, allval);  //遍历右节点
}

//后序遍历
void postOrder(Node* root, vector<int> &allval)
{
	if (!root)
	{
		return;
	}
	postOrder(root->left, allval);   //遍历左节点
	postOrder(root->right, allval);  //遍历右节点
	allval.push_back(root->value);  //遍历根节点
} 

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);
	//         1
	//      2     3
	//   4    5

	cout << "前序遍历结果:" << endl;  //12453
	vector<int> ves;
	preOrder(root, ves);
	for (int i = 0; i < ves.size(); i++)
	{
		cout << ves.at(i) << " ";
	}
	cout << endl;

	cout << "中序遍历结果:" << endl;  //42513
	vector<int> ves1;
	inOrder(root, ves1);
	for (int i = 0; i < ves1.size(); i++)
	{
		cout << ves1.at(i) << " ";
	}
	cout << endl;

	cout << "后序遍历结果:" << endl;  //45231
	vector<int> ves2;
	postOrder(root, ves2);
	for (int i = 0; i < ves2.size(); i++)
	{
		cout << ves2.at(i) << " ";
	}
	cout << endl;

	system("pause");
	return 0;
}

在这里插入图片描述

  • 13
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是二叉树前中后序遍历C++ 代码: ```c++ #include <iostream> #include <stack> using namespace std; struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; // 序遍历 void preorderTraversal(TreeNode* root) { if (root == NULL) return; cout << root->val << " "; preorderTraversal(root->left); preorderTraversal(root->right); } // 序遍历 void inorderTraversal(TreeNode* root) { if (root == NULL) return; inorderTraversal(root->left); cout << root->val << " "; inorderTraversal(root->right); } // 后序遍历 void postorderTraversal(TreeNode* root) { if (root == NULL) return; postorderTraversal(root->left); postorderTraversal(root->right); cout << root->val << " "; } // 非递归序遍历 void preorderTraversalNonRecursive(TreeNode* root) { if (root == NULL) return; stack<TreeNode*> s; s.push(root); while (!s.empty()) { TreeNode* curr = s.top(); s.pop(); cout << curr->val << " "; if (curr->right != NULL) s.push(curr->right); if (curr->left != NULL) s.push(curr->left); } } // 非递归序遍历 void inorderTraversalNonRecursive(TreeNode* root) { if (root == NULL) return; stack<TreeNode*> s; TreeNode* curr = root; while (curr != NULL || !s.empty()) { while (curr != NULL) { s.push(curr); curr = curr->left; } curr = s.top(); s.pop(); cout << curr->val << " "; curr = curr->right; } } // 非递归后序遍历 void postorderTraversalNonRecursive(TreeNode* root) { if (root == NULL) return; stack<TreeNode*> s1, s2; s1.push(root); while (!s1.empty()) { TreeNode* curr = s1.top(); s1.pop(); s2.push(curr); if (curr->left != NULL) s1.push(curr->left); if (curr->right != NULL) s1.push(curr->right); } while (!s2.empty()) { cout << s2.top()->val << " "; s2.pop(); } } int main() { TreeNode* root = new TreeNode(1); root->left = new TreeNode(2); root->right = new TreeNode(3); root->left->left = new TreeNode(4); root->left->right = new TreeNode(5); root->right->left = new TreeNode(6); root->right->right = new TreeNode(7); cout << "Preorder traversal: "; preorderTraversal(root); cout << endl; cout << "Inorder traversal: "; inorderTraversal(root); cout << endl; cout << "Postorder traversal: "; postorderTraversal(root); cout << endl; cout << "Non-recursive preorder traversal: "; preorderTraversalNonRecursive(root); cout << endl; cout << "Non-recursive inorder traversal: "; inorderTraversalNonRecursive(root); cout << endl; cout << "Non-recursive postorder traversal: "; postorderTraversalNonRecursive(root); cout << endl; return 0; } ``` 输出结果为: ``` Preorder traversal: 1 2 4 5 3 6 7 Inorder traversal: 4 2 5 1 6 3 7 Postorder traversal: 4 5 2 6 7 3 1 Non-recursive preorder traversal: 1 2 4 5 3 6 7 Non-recursive inorder traversal: 4 2 5 1 6 3 7 Non-recursive postorder traversal: 4 5 2 6 7 3 1 ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值