代码如下:
#include<iostream>
#include <queue>
#include <stack>
using namespace std;class BinTree
{
private:class TreeNode{public:int data;TreeNode *left;TreeNode *right;TreeNode ():data(0),left(nullptr),right(nullptr){}TreeNode(int e):data(e),left(nullptr),right(nullptr){}};private:TreeNode *root;public:BinTree():root(nullptr){}void pre_in_buildTree(int *pre, int *in, int n){if (n <= 0){cout << "The tree is null" << endl;return;}root = pre_in_dfsTree(pre, in, 0, n - 1, 0, n - 1);}void in_post_buildTree(int *in, int *post, int n){if (n <= 0){cout << "The tree is null" << endl;return;}root = in_post_dfsTree(in, post, 0, n - 1, 0, n - 1);}void preOrder(){predfsTree(root);cout << endl;}void inOrder(){indfsTree(root);cout << endl;}void postOrder(){postdfsTree(root);cout << endl;}void levelOrder()//层次遍历{if (root == nullptr){cout << "The tree is null" << endl;return;}queue<TreeNode *> q;q.push(root);while (!q.empty()){TreeNode *t = q.front();cout << t->data << " ";q.pop();if (t->left) q.push(t->left);if (t->right) q.push(t->right);}cout << endl;}void preOrder_Stack()//先序遍历(非递归){TreeNode *p = root;stack<TreeNode *> s;while (p || !s.empty()){while (p){cout << p->data << " ";s.push(p);p = p->left;}if (!s.empty()){p = s.top();s.pop();p = p->right;}}cout << endl;}void inOrder_Stack()//中序遍历(非递归){TreeNode *p = root;stack<TreeNode *> s;while (p || !s.empty()){while (p){s.push(p);p = p->left;}if (!s.empty()){p = s.top();cout << p->data << " ";s.pop();p = p->right;}}cout << endl;}void postOrder_Stack()//后序遍历(非递归){stack<TreeNode *>s;s.push(root);while (!s.empty()){TreeNode *t = s.top();s.pop();if (t){s.push(t);s.push(nullptr);if (t->right) s.push(t->right);if (t->left) s.push(t->left);}else{t = s.top();s.pop();cout << t->data << " ";}}cout << endl;}private:TreeNode* pre_in_dfsTree(int *pre, int *in, int o1, int h1, int o2, int h2)//传入先序遍历和中序遍历构造一颗树{if (o1 > h1) return nullptr;int val = pre[o1];TreeNode *root = new TreeNode(val);if (o1 == h1) return root;int mid = 0;while (in[o2 + mid] != val) mid++;root->left = pre_in_dfsTree(pre, in,o1+1 ,o1+mid ,o2,o2+mid-1 );root->right = pre_in_dfsTree(pre, in,o1+mid+1 ,h1 , o2 + mid + 1, h2);return root;}TreeNode * in_post_dfsTree(int *in, int *post, int o1, int h1, int o2, int h2)//传入中序遍历和后序遍历构造一颗树{if (o1 > h1) return nullptr;int val = post[h2];TreeNode *root = new TreeNode(val);if (o1 == h1) return root;int mid = 0;while (in[o1 + mid] != val) mid++;root->left = in_post_dfsTree(in, post, o1, o1+mid-1, o2, o2+mid-1);root->right = in_post_dfsTree(in, post,o1+mid+1 ,h1, o2 + mid, h2-1);return root;}void predfsTree(TreeNode *root)//先序遍历{if (root == nullptr) return;cout << root->data << " ";predfsTree(root->left);predfsTree(root->right);}void indfsTree(TreeNode *root)//中序遍历{if (root == nullptr) return;indfsTree(root->left);cout << root->data << " ";indfsTree(root->right);}void postdfsTree(TreeNode *root)//后序遍历{if (root == nullptr) return;postdfsTree(root->left);postdfsTree(root->right);cout << root->data << " ";}};int main()
{BinTree t1;int pre[] = { 3,9,20,15,7 };int in[] = { 9,3,15,20,7 };int post[] = { 9,15,7,20,3 };t1.pre_in_buildTree(pre, in,5);BinTree t2;t2.in_post_buildTree(in, post, 5);t1.levelOrder();t2.levelOrder();t1.preOrder();t2.preOrder();t1.preOrder_Stack();t1.inOrder();t2.inOrder();t1.inOrder_Stack();t1.postOrder();t2.postOrder();t1.postOrder_Stack();return 0;
}
测试结果如下: