二叉树的遍历
struct Node
{
int value;
Node* left;
Node* right;
Node(int val):value(val),left(NULL),right(NULL)
{
}
};
#include <windows.h>
#include <stack>
#include <list>
using namespace std;
void preOrder(Node* root)
{
if(root != NULL)
{
printf("%d-",root->value);
preOrder(root->left);
preOrder(root->right);
}
}
void inOrder(Node* root)
{
if(root != NULL)
{
inOrder(root->left);
printf("%d-",root->value);
inOrder(root->right);
}
}
void postOrder(Node* root)
{
if(root != NULL)
{
postOrder(root->left);
postOrder(root->right);
printf("%d-",root->value);
}
}
void preOrder_2(Node* root)
{
stack<Node*> stk;
Node* cur = root;
while(stk.size() >0 || cur != NULL)
{
while(cur != NULL)
{
printf("%d-",cur->value);
stk.push(cur);
cur = cur->left;
}
if(stk.size() >0)
{
cur = stk.top();
stk.pop();
cur = cur->right;
}
}
}
void inOrder_2(Node* root)
{
stack<Node*> stk;
Node* cur = root;
while(stk.size() >0 || cur != NULL)
{
while(cur != NULL)
{
stk.push(cur);
cur = cur->left;
}
if(stk.size() >0)
{
cur = stk.top();
printf("%d-",cur->value);
stk.pop();
cur = cur->right;
}
}
}
void postOrder_2(Node* root,stack<Node*>& postStk)
{
stack<Node*> stk;
Node* cur = root;
while(stk.size() >0 || cur != NULL)
{
while(cur != NULL)
{
stk.push(cur);
postStk.push(cur);
cur = cur->right;
}
if(stk.size() >0)
{
cur = stk.top();
stk.pop();
cur = cur->left;
}
}
}
void levelOrder(Node* root)
{
Node* cur = root;
list<Node*> nList;
nList.push_back(cur);
while(nList.size() >0)
{
cur = nList.front();
nList.pop_front();
printf("%d-",cur->value);
if(cur->left != NULL)
{
nList.push_back(cur->left);
}
if(cur->right != NULL)
{
nList.push_back(cur->right);
}
}
}
void createTree(Node*& root)
{
root = new Node(0);
Node* n1 = new Node(1);
Node* n2 = new Node(2);
Node* n3 = new Node(3);
Node* n4 = new Node(4);
Node* n5 = new Node(5);
Node* n6 = new Node(6);
Node* n7 = new Node(7);
Node* n8 = new Node(8);
root->left = n1;
root->right = n2;
n1->left = n3;
n1->right = n4;
n2->left = n5;
n2->right = n6;
n3->left = n7;
n3->right = n8;
}
int main(int argc, char* argv[])
{
Node* root;
createTree(root);
preOrder(root);
printf("\n");
preOrder_2(root);
printf("\n");
inOrder(root);
printf("\n");
inOrder_2(root);
printf("\n");
postOrder(root);
printf("\n");
stack<Node*> postStk;
postOrder_2(root,postStk);
while(postStk.size() >0)
{
printf("%d-",postStk.top()->value);
postStk.pop();
}
printf("\n");
levelOrder(root);
system("pause");
return 0;
}
{
int value;
Node* left;
Node* right;
Node(int val):value(val),left(NULL),right(NULL)
{
}
};
#include <windows.h>
#include <stack>
#include <list>
using namespace std;
void preOrder(Node* root)
{
if(root != NULL)
{
printf("%d-",root->value);
preOrder(root->left);
preOrder(root->right);
}
}
void inOrder(Node* root)
{
if(root != NULL)
{
inOrder(root->left);
printf("%d-",root->value);
inOrder(root->right);
}
}
void postOrder(Node* root)
{
if(root != NULL)
{
postOrder(root->left);
postOrder(root->right);
printf("%d-",root->value);
}
}
void preOrder_2(Node* root)
{
stack<Node*> stk;
Node* cur = root;
while(stk.size() >0 || cur != NULL)
{
while(cur != NULL)
{
printf("%d-",cur->value);
stk.push(cur);
cur = cur->left;
}
if(stk.size() >0)
{
cur = stk.top();
stk.pop();
cur = cur->right;
}
}
}
void inOrder_2(Node* root)
{
stack<Node*> stk;
Node* cur = root;
while(stk.size() >0 || cur != NULL)
{
while(cur != NULL)
{
stk.push(cur);
cur = cur->left;
}
if(stk.size() >0)
{
cur = stk.top();
printf("%d-",cur->value);
stk.pop();
cur = cur->right;
}
}
}
void postOrder_2(Node* root,stack<Node*>& postStk)
{
stack<Node*> stk;
Node* cur = root;
while(stk.size() >0 || cur != NULL)
{
while(cur != NULL)
{
stk.push(cur);
postStk.push(cur);
cur = cur->right;
}
if(stk.size() >0)
{
cur = stk.top();
stk.pop();
cur = cur->left;
}
}
}
void levelOrder(Node* root)
{
Node* cur = root;
list<Node*> nList;
nList.push_back(cur);
while(nList.size() >0)
{
cur = nList.front();
nList.pop_front();
printf("%d-",cur->value);
if(cur->left != NULL)
{
nList.push_back(cur->left);
}
if(cur->right != NULL)
{
nList.push_back(cur->right);
}
}
}
void createTree(Node*& root)
{
root = new Node(0);
Node* n1 = new Node(1);
Node* n2 = new Node(2);
Node* n3 = new Node(3);
Node* n4 = new Node(4);
Node* n5 = new Node(5);
Node* n6 = new Node(6);
Node* n7 = new Node(7);
Node* n8 = new Node(8);
root->left = n1;
root->right = n2;
n1->left = n3;
n1->right = n4;
n2->left = n5;
n2->right = n6;
n3->left = n7;
n3->right = n8;
}
int main(int argc, char* argv[])
{
Node* root;
createTree(root);
preOrder(root);
printf("\n");
preOrder_2(root);
printf("\n");
inOrder(root);
printf("\n");
inOrder_2(root);
printf("\n");
postOrder(root);
printf("\n");
stack<Node*> postStk;
postOrder_2(root,postStk);
while(postStk.size() >0)
{
printf("%d-",postStk.top()->value);
postStk.pop();
}
printf("\n");
levelOrder(root);
system("pause");
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
2012-03-07 理解IP和端口