二叉树三种遍历方式
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; }
相关文章推荐
- 二叉树的常见方法及三种遍历方式 Java 实现
- 二叉树的三种遍历方式java实现
- 二叉树三种遍历方式非递归实现
- 二叉树的三种遍历方式java实现
- 二叉树的三种遍历方式
- C预言的二叉树的创建和三种遍历方式
- 二叉树的三种遍历方式(递归和非递归的实现方法)
- 重温数据结构:二叉树的常见方法及三种遍历方式 Java 实现
- 详细讲解二叉树三种遍历方式的递归与非递归实现
- 二叉树三种递归遍历方式
- 二叉树的java构建和三种遍历方式
- 二叉树的前序、中序、后序三种遍历的六种实现方式(递归、非递归)(C++)
- 二叉树的三种遍历方式的递归实现和迭代实现
- 详细讲解二叉树三种遍历方式的递归与非递归实现
- 用Java实现二叉树的三种遍历方式
- 二叉树的常见方法及三种遍历方式 Java 实现
- Java三种遍历方式打印二叉树(递归实现)
- 二叉树的三种遍历方式
- 重温数据结构:二叉树的常见方法及三种遍历方式 Java 实现
- Java中创建二叉树并实现三种遍历方式