二叉树的中序、前序、后序以及层序的非递归遍历并求树的深度和结点总数

本文介绍了如何通过非递归方式实现二叉树的中序、前序、后序遍历,并详细讲解了如何计算树的深度和结点总数,包含相关代码实现。
摘要由CSDN通过智能技术生成

代码:

#include<iostream>  
#include<vector>
#include<queue>
#include<deque>
#include<stack>
using namespace std;  
  
typedef struct Node  
{  
    int element;  
    struct Node * left;  
    struct Node * right;      
}TreeNode,*BSTree,*ptrnode;  
  
ptrnode Insert(int x,BSTree &T)  
{  
    if(NULL==T)  
    {  
        T=(ptrnode)malloc(sizeof(TreeNode));  
        if(T==NULL)  
        {  
            cout<<"out of space!"<<endl;  
            exit(0);  
        }  
        T->element=x;  
        T->left=T->right=NULL;  
    }  
    else if(x<T->element)  
    {  
        T->left=Insert(x,T->left);  
    }  
    else if(x>T->element)  
    {  
        T->right=Insert(x,T->right);  
    }  
    return T;  
}  
ptrnode  Find(int x,BSTree &T)  
{  
    if(NULL==T)  
        return NULL;  
    if(x<T->element)  
        return Find(x,T->left);  
    else if(x>T->element)  
        return Find(x,T->right);  
    else  
        return T;  
} 
void MakeEmpty(BSTree &T)//destroy the BSTree  
{  
    if(NULL==T)  
        return ;  
    else  
    {  
        if(T->left!=NULL)  
            MakeEmpty(T->left)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值