C++迭代实现二叉树的前序遍历、中序遍历和后序遍历
1. 二叉树的创建
struct TreeNode {
TreeNode() : m_val(0), m_left_node(NULL), m_right_node(NULL) { }
TreeNode(int val) : m_val(val), m_left_node(NULL), m_right_node(NULL) { }
TreeNode(int val, TreeNode* left_node, TreeNode* right_node) : m_val(val), m_left_node(left_node), m_right_node(right_node) { }
int m_val;
TreeNode* m_left_node;
TreeNode* m_right_node;
};
2.迭代遍历方法类实现
class Solution {
public:
std::vector<int> Traversal(TreeNode* root) {
std::vector<int> result;
TraversalResult3(root, result);
return result;
}
void TraversalResult1(TreeNode* root, std::vector<int>& result) {
std::stack<TreeNode*> stack;
if (!root) return;
stack.push(root);
while (!stack.empty()) {
TreeNode* cur_node = stack.top();
stack.pop();
result.push_back(cur_node->m_val);
if (cur_node->m_right_node) stack.push(cur_node->m_right_node);
if (cur_node->m_left_node) stack.push(cur_node->m_left_node);
}
}
void TraversalResult2(TreeNode* root, std::vector<int>& result) {
std::stack<TreeNode*> stack;
if (!root) return;
TreeNode* cur_node = root;
while (cur_node || !stack.empty()) {
if (cur_node) {
stack.push(cur_node);
cur_node = cur_node->m_left_node;
}
else {
cur_node = stack.top();
stack.pop();
result.push_back(cur_node->m_val);
cur_node = cur_node->m_right_node;
}
}
}
void TraversalResult3(TreeNode* root, std::vector<int>& result) {
std::stack<TreeNode*> stack;
if (!root) return;
stack.push(root);
while (!stack.empty()) {
TreeNode* cur_node = stack.top();
stack.pop();
result.push_back(cur_node->m_val);
if (cur_node->m_left_node) stack.push(cur_node->m_left_node);
if (cur_node->m_right_node) stack.push(cur_node->m_right_node);
}
ReVector(result);
}
void ReVector(std::vector<int>& result) {
int start = 0, end = result.size() - 1;
while (start <= end) {
int tmp = result[end];
result[end] = result[start];
result[start] = tmp;
start++;
end--;
}
}
void FreeSpace(TreeNode* root) {
if (!root) return;
if (root->m_left_node) FreeSpace(root->m_left_node);
if (root->m_right_node) FreeSpace(root->m_right_node);
delete root;
}
};
3.整体代码
#include <iostream>
#include <vector>
#include <stack>
struct TreeNode {
TreeNode() : m_val(0), m_left_node(NULL), m_right_node(NULL) { }
TreeNode(int val) : m_val(val), m_left_node(NULL), m_right_node(NULL) { }
TreeNode(int val, TreeNode* left_node, TreeNode* right_node) : m_val(val), m_left_node(left_node), m_right_node(right_node) { }
int m_val;
TreeNode* m_left_node;
TreeNode* m_right_node;
};
class Solution {
public:
std::vector<int> Traversal(TreeNode* root) {
std::vector<int> result;
TraversalResult3(root, result);
return result;
}
void TraversalResult1(TreeNode* root, std::vector<int>& result) {
std::stack<TreeNode*> stack;
if (!root) return;
stack.push(root);
while (!stack.empty()) {
TreeNode* cur_node = stack.top();
stack.pop();
result.push_back(cur_node->m_val);
if (cur_node->m_right_node) stack.push(cur_node->m_right_node);
if (cur_node->m_left_node) stack.push(cur_node->m_left_node);
}
}
void TraversalResult2(TreeNode* root, std::vector<int>& result) {
std::stack<TreeNode*> stack;
if (!root) return;
TreeNode* cur_node = root;
while (cur_node || !stack.empty()) {
if (cur_node) {
stack.push(cur_node);
cur_node = cur_node->m_left_node;
}
else {
cur_node = stack.top();
stack.pop();
result.push_back(cur_node->m_val);
cur_node = cur_node->m_right_node;
}
}
}
void TraversalResult3(TreeNode* root, std::vector<int>& result) {
std::stack<TreeNode*> stack;
if (!root) return;
stack.push(root);
while (!stack.empty()) {
TreeNode* cur_node = stack.top();
stack.pop();
result.push_back(cur_node->m_val);
if (cur_node->m_left_node) stack.push(cur_node->m_left_node);
if (cur_node->m_right_node) stack.push(cur_node->m_right_node);
}
ReVector(result);
}
void ReVector(std::vector<int>& result) {
int start = 0, end = result.size() - 1;
while (start <= end) {
int tmp = result[end];
result[end] = result[start];
result[start] = tmp;
start++;
end--;
}
}
void FreeSpace(TreeNode* root) {
if (!root) return;
if (root->m_left_node) FreeSpace(root->m_left_node);
if (root->m_right_node) FreeSpace(root->m_right_node);
delete root;
}
};
int main() {
TreeNode* root = new TreeNode(5, new TreeNode(6, new TreeNode(8), NULL), new TreeNode(7, NULL, new TreeNode(9)));
Solution solution;
std::vector<int> result = solution.Traversal(root);
for (int i = 0; i < result.size(); ++i) std::cout << result[i] << " ";
std::cout << std::endl;
solution.FreeSpace(root);
return 0;
}