求二叉树的高度(可运行)

输入二叉树为:ABD##E##C##。

运行环境:main.cpp

运行结果:3

cpp 复制代码
#include "bits/stdc++.h"
using namespace std;
typedef struct BiTNode{
    char data;
    struct BiTNode *lchild,*rchild;
    int tag;
}BiTNode,*BiTree;

void createTree(BiTree  &t){
    char ch;
    ch=getchar();
    if (ch=='#') t=NULL;
    else{
        t=(BiTNode *) malloc(sizeof (BiTNode));
        t->data=ch;
        t->tag=0;
        t->lchild=NULL;
        t->rchild=NULL;
        createTree(t->lchild);
        createTree(t->rchild);
    }
}
int btDepth(BiTree t){
    if (!t)
        return 0;
    int front=-1,rear=-1;
    int last=0,level=0;
    BiTree Q[100];
    Q[++rear]=t;
    BiTree p;
    while (front<rear){
        p=Q[++front];
        if (p->lchild)
            Q[++rear]=p->lchild;
        if (p->rchild)
            Q[++rear]=p->rchild;
        if (front==last){
            level++;
            last=rear;
        }
    }
    return level;
}
int main() {
  BiTree t;
  createTree(t);
  printf("%d", btDepth(t));

}
相关推荐
程序员龙一5 小时前
C++之static_cast关键字
开发语言·c++·static_cast
奶茶树5 小时前
【C++/STL】map和multimap的使用
开发语言·c++·stl
云知谷6 小时前
【C/C++基本功】C/C++江湖风云录:void* 的江湖传说
c语言·开发语言·c++·软件工程·团队开发
ShineWinsu7 小时前
对于数据结构:堆的超详细保姆级解析—上
数据结构·c++·算法·计算机·二叉树·顺序表·
im_AMBER8 小时前
Leetcode 46
c语言·c++·笔记·学习·算法·leetcode
QX_hao8 小时前
【Go】--文件和目录的操作
开发语言·c++·golang
卡提西亚8 小时前
C++笔记-20-对象特性
开发语言·c++·笔记
三掌柜6669 小时前
C++ 零基础入门与冒泡排序深度实现
java·开发语言·c++
沐怡旸9 小时前
【穿越Effective C++】条款15:在资源管理类中提供对原始资源的访问——封装与兼容性的平衡艺术
c++·面试
利刃大大10 小时前
【高并发服务器:HTTP应用】十五、HttpRequest请求模块 && HttpResponse响应模块设计
服务器·c++·http·项目