二叉树遍历非递归版本(C++)
前、中、后序遍历
static void preOrderUnRecur(Node* head) {
printf("pre-order: ");
if (head != NULL) {
stack<Node*> stack;
stack.push(head);
while (!stack.empty()) {
head = stack.top();
stack.pop();
cout << head->value << " ";
if (head->right != NULL) {
stack.push(head->right);
}
if (head->left != NULL) {
stack.push(head->left);
}
}
}
cout << "\n";
}
static void inOrderUnRecur(Node* head) {
printf("in-order: ");
if (head != NULL) {
stack<Node*> stack;
while (!stack.empty() || head != NULL) {
if (head != NULL) {
stack.push(head);
head = head->left;
}
else {
head = stack.top();
stack.pop();
cout << head->value << " ";
head = head->right;
}
}
}
cout << "\n";
}
static void posOrderUnRecur1(Node* head) {
printf("pos-order: ");
if (head != NULL) {
stack<Node*> s1;
stack<Node*> s2;
s1.push(head);
while (!s1.empty()) {
head = s1.top();
s1.pop();
s2.push(head);
if (head->left != NULL) {
s1.push(head->left);
}
if (head->right != NULL) {
s1.push(head->right);
}
}
while (!s2.empty()) {
head = s2.top();
s2.pop();
cout << head->value << " ";
}
}
cout << "\n";
}
全部包括递归、非递归的完整代码
#include <iostream>
#include<stack>
using namespace std;
struct Node {
int value=0;
Node* left=NULL;
Node* right = NULL;
Node(int data) {
value = data;
}
};
class Code_01_PreInPosTraversal
{
public :
static void preOrderRecur(Node* head) {
if (head == NULL) {
return;
}
cout << head->value << " ";
preOrderRecur(head->left);
preOrderRecur(head->right);
}
static void inOrderRecur(Node* head)
{
if (head == NULL) {
return;
}
inOrderRecur(head->left);
cout << head->value << " ";
inOrderRecur(head->right);
}
static void posOrderRecur(Node* head) {
if (head == NULL) {
return;
}
posOrderRecur((*head).left);
posOrderRecur(head->right);
cout << head->value << " ";
}
static void preOrderUnRecur(Node* head) {
printf("pre-order: ");
if (head != NULL) {
stack<Node*> stack;
stack.push(head);
while (!stack.empty()) {
head = stack.top();
stack.pop();
cout << head->value << " ";
if (head->right != NULL) {
stack.push(head->right);
}
if (head->left != NULL) {
stack.push(head->left);
}
}
}
cout << "\n";
}
static void inOrderUnRecur(Node* head) {
printf("in-order: ");
if (head != NULL) {
stack<Node*> stack;
while (!stack.empty() || head != NULL) {
if (head != NULL) {
stack.push(head);
head = head->left;
}
else {
head = stack.top();
stack.pop();
cout << head->value << " ";
head = head->right;
}
}
}
cout << "\n";
}
static void posOrderUnRecur1(Node* head) {
printf("pos-order: ");
if (head != NULL) {
stack<Node*> s1;
stack<Node*> s2;
s1.push(head);
while (!s1.empty()) {
head = s1.top();
s1.pop();
s2.push(head);
if (head->left != NULL) {
s1.push(head->left);
}
if (head->right != NULL) {
s1.push(head->right);
}
}
while (!s2.empty()) {
head = s2.top();
s2.pop();
cout << head->value << " ";
}
}
cout << "\n";
}
};
int main()
{
Code_01_PreInPosTraversal PreInPosTraversal;
Node* head= new Node(5);
head->left = new Node(3);
head->right = new Node(8);
head->left->left = new Node(2);
head->left->right = new Node(4);
head->left->left->left = new Node(1);
head->right->left = new Node(7);
head->right->left->left = new Node(6);
head->right->right = new Node(10);
head->right->right->left = new Node(9);
head->right->right->right = new Node(11);
// recursive
printf("==============recursive==============\n");
printf("pre-order: ");
PreInPosTraversal.preOrderRecur(head);
cout << "\n";
printf("in-order: ");
PreInPosTraversal.inOrderRecur(head);
cout << "\n";
printf("pos-order: ");
PreInPosTraversal.posOrderRecur(head);
cout << "\n";
printf("============unrecursive=============");
cout << "\n";
PreInPosTraversal.preOrderUnRecur(head);
PreInPosTraversal.inOrderUnRecur(head);
PreInPosTraversal.posOrderUnRecur1(head);
}