您的位置:首页 > 其它

二叉树三种遍历方式

2015-05-04 21:17 246 查看
#include <iostream>
#include <stack>
#include <set>
#include <vector>
#include <algorithm>
using namespace std;

#define ITE

struct TreeNode
{
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int v) : val(v), left(NULL), right(NULL) {}
};

#ifdef REC
void preorder(TreeNode* root)
{
if (!root) return;
cout << root->val << " ";
preorder(root->left);
preorder(root->right);
}

void inorder(TreeNode* root)
{
if (!root) return;
inorder(root->left);
cout << root->val << " ";
inorder(root->right);
}

void postorder(TreeNode* root)
{
if (!root) return;
postorder(root->left);
postorder(root->right);
cout << root->val << " ";
}

#else if (define ITE)
void preorder(TreeNode* root)
{
if (!root) return;
stack<TreeNode*> sta;
sta.push(root);
while (!sta.empty())
{
TreeNode* temp = sta.top();
sta.pop();
cout << temp->val << " ";
if (temp->right) sta.push(temp->right);
if (temp->left)  sta.push(temp->left);
}
}

void inorder(TreeNode* root)
{
if (!root) return;
stack<TreeNode*> sta;
set<TreeNode*> visited;
sta.push(root);
while (!sta.empty())
{
TreeNode* temp = sta.top();
if (visited.find(temp) == visited.end())
{
visited.insert(temp);
if (temp->left) sta.push(temp->left);
}
else
{
sta.pop();
cout << temp->val << " ";
if (temp->right) sta.push(temp->right);
}
}
}

void postorder(TreeNode* root)
{
if (!root) return;
vector<int> vec;
stack<TreeNode*> sta;
sta.push(root);
while (!sta.empty())
{
TreeNode* temp = sta.top();
sta.pop();
vec.push_back(temp->val);
if (temp->left) sta.push(temp->left);
if (temp->right) sta.push(temp->right);
}
reverse(vec.begin(), vec.end());
for (int i = 0; i < vec.size(); ++i)
cout << vec[i] << " ";
}
#endif

int main()
{
TreeNode* ptn1 = new TreeNode(1);
TreeNode* ptn2 = new TreeNode(2);
TreeNode* ptn3 = new TreeNode(3);
TreeNode* ptn4 = new TreeNode(4);
TreeNode* ptn5 = new TreeNode(5);
TreeNode* ptn6 = new TreeNode(6);
ptn1->left = ptn2;
ptn1->right = ptn3;
ptn2->left = ptn4;
ptn2->right = ptn5;
ptn3->right = ptn6;

preorder(ptn1);
cout << endl;

inorder(ptn1);
cout << endl;

postorder(ptn1);
cout << endl;

system("pause");
return 0;
}


另:二叉树构造

问题:给出前序遍历和中序遍历,生成二叉树(本题的前提是序列中每个数都不同)

方法:通过查找两个序列中相同的元素,将序列分割,递归构造

struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

TreeNode* construct(vector<int> &pre, int i, int j, vector<int> &in, int m, int n);

TreeNode* reconstructBT(vector<int> &pre, vector<int> &in)
{
if (pre.empty() || in.empty() || pre.size() != in.size()) return NULL;
return construct(pre, 0, pre.size() - 1, in, 0, in.size() - 1);
}

TreeNode* construct(vector<int> &pre, int i, int j, vector<int> &in, int m, int n)
{
if (i > j || m > n) return NULL;
int k = m;
while (k <= n && in[k] != pre[i]) ++k;
TreeNode* node = new TreeNode(in[k]);
node->left = construct(pre, i + 1, i + k - m, in, m, k - 1);
node->right = construct(pre, i + k - m + 1, j, in, k + 1, n);
return node;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: