先是递归的三种:
void preOrderPrint(TreeNode* head){
if(head == nullptr){
return ;
}
cout << head->value << endl;
preOrderPrint(head->left);
preOrderPrint(head->right);
}
void inOrderPrint(TreeNode* head){
if(head!= nullptr){
inOrderPrint(head->left);
cout << head->value <<endl;
inOrderPrint(head->right);
}
}
void posOrderPrint(TreeNode* head){
if(head != nullptr){
posOrderPrint(head->left);
posOrderPrint(head->right);
cout << head->value << endl;
}
}
非递归的先序遍历:
void preOrderUnRecur(TreeNode* head){
//当前节点不为空的情况下:先把头结点入栈,然后while循环,
//若栈不为空,则出栈并输出,然后若右节点不为空,有节点先入栈,左节点不为空,左节点入栈
if(head != nullptr){
stack<TreeNode*> s;
s.push(head);
TreeNode* help;
while(!s.empty()){
help = s.top();
s.pop();
cout << help->value;
if(help->right != nullptr){
s.push(help->right);
}
if(help->left != nullptr){
s.push(help->left);
}
}
}
}
非递归的中序:
void inOrderUnRecur(TreeNode* head){
if(head != nullptr){
stack<TreeNode*> s;
TreeNode* help;
while(!s.empty() || head != nullptr){
//一直往左走,不能走的时候,回栈中取出一个元素,然后去它的右边 ,然后继续往左走
if(head != nullptr){
s.push(head);
head = head->left;
}else{
help = s.top();
s.pop();
cout << help->value << endl;
head = help->right;
}
}
}
}
非递归的后序遍历:
void posOrderUnRecur(TreeNode* head){
if(head != nullptr){
stack<TreeNode*> s;
stack<TreeNode*> help_s;
TreeNode* help;
s.push(head);
//类似于非递归版的先序遍历, 改变if的左右子树变换,然后用栈反序输出打印
while(!s.empty()){
help = s.top();
s.pop();
help_s.push(help);
if(help->left != nullptr){
s.push(help->left);
}
if(help->right != nullptr){
s.push(help->right);
}
}
while(!help_s.empty()){
help = help_s.top();
help_s.pop();
cout << help->value << endl;
}
}
}
整体的代码+测试:
#include<stack>
#include<iostream>
using namespace std;
struct TreeNode{
int value;
TreeNode* left;
TreeNode* right;
};
TreeNode* getTree(int arr[],int length,int i);
void preOrderPrint(TreeNode* head);
void preOrderUnRecur(TreeNode* head);
void inOrderPrint(TreeNode* head);
void inOrderUnRecur(TreeNode* head);
void posOrderPrint(TreeNode* head);
void posOrderUnRecur(TreeNode* head);
int main(){
int arr[] = {1,2,3,4,5,6,7};
int length = sizeof(arr)/sizeof(arr[0]);
TreeNode* head = getTree(arr, length,0);
// preOrderPrint(head);
// preOrderUnRecur(head);
// inOrderPrint(head);
// inOrderUnRecur(head);
// posOrderPrint(head);
posOrderUnRecur(head);
return 0;
}
TreeNode* getTree(int arr[],int length,int i){
if(i < length){
TreeNode* node = new TreeNode();
node->value = arr[i];
if((2*i+1) < length){
node->left = getTree(arr, length, 2*i+1);
}else{
node->left = nullptr;
}
if((2*i+2)<length){
node->right = getTree(arr, length,2*i+2);
}else{
node->right = nullptr;
}
return node;
}
return nullptr;
}
void preOrderPrint(TreeNode* head){
if(head == nullptr){
return ;
}
cout << head->value << endl;
preOrderPrint(head->left);
preOrderPrint(head->right);
}
void preOrderUnRecur(TreeNode* head){
//当前节点不为空的情况下:先把头结点入栈,然后while循环,
//若栈不为空,则出栈并输出,然后若右节点不为空,有节点先入栈,左节点不为空,左节点入栈
if(head != nullptr){
stack<TreeNode*> s;
s.push(head);
TreeNode* help;
while(!s.empty()){
help = s.top();
s.pop();
cout << help->value;
if(help->right != nullptr){
s.push(help->right);
}
if(help->left != nullptr){
s.push(help->left);
}
}
}
}
void inOrderPrint(TreeNode* head){
if(head!= nullptr){
inOrderPrint(head->left);
cout << head->value <<endl;
inOrderPrint(head->right);
}
}
void inOrderUnRecur(TreeNode* head){
if(head != nullptr){
stack<TreeNode*> s;
TreeNode* help;
while(!s.empty() || head != nullptr){
//一直往左走,不能走的时候,回栈中取出一个元素,然后去它的右边
if(head != nullptr){
s.push(head);
head = head->left;
}else{
help = s.top();
s.pop();
cout << help->value << endl;
head = help->right;
}
}
}
}
void posOrderPrint(TreeNode* head){
if(head != nullptr){
posOrderPrint(head->left);
posOrderPrint(head->right);
cout << head->value << endl;
}
}
void posOrderUnRecur(TreeNode* head){
if(head != nullptr){
stack<TreeNode*> s;
stack<TreeNode*> help_s;
TreeNode* help;
s.push(head);
//类似于非递归版的先序遍历, 改变if的左右子树变换,然后用栈反序输出打印
while(!s.empty()){
help = s.top();
s.pop();
help_s.push(help);
if(help->left != nullptr){
s.push(help->left);
}
if(help->right != nullptr){
s.push(help->right);
}
}
while(!help_s.empty()){
help = help_s.top();
help_s.pop();
cout << help->value << endl;
}
}
}