随笔 - 576  文章 - 0  评论 - 62  阅读 - 219万

二叉树的遍历

复制代码
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;
}
 
复制代码
posted on   Andy Niu  阅读(282)  评论(0编辑  收藏  举报
编辑推荐:
· 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和端口
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示