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

输入二叉树为: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));

}
相关推荐
岁忧37 分钟前
(LeetCode 面试经典 150 题 ) 11. 盛最多水的容器 (贪心+双指针)
java·c++·算法·leetcode·面试·go
蜉蝣之翼❉6 小时前
CRT 不同会导致 fopen 地址不同
c++·mfc
aramae6 小时前
C++ -- STL -- vector
开发语言·c++·笔记·后端·visual studio
lixzest6 小时前
C++ Lambda 表达式详解
服务器·开发语言·c++·算法
_Chipen7 小时前
C++基础问题
开发语言·c++
灿烂阳光g7 小时前
OpenGL 2. 着色器
c++·opengl
AA陈超8 小时前
虚幻引擎UE5专用服务器游戏开发-20 添加基础能力类与连招能力
c++·游戏·ue5·游戏引擎·虚幻
mit6.8249 小时前
[Meetily后端框架] AI摘要结构化 | `SummaryResponse`模型 | Pydantic库 | vs marshmallow库
c++·人工智能·后端
R-G-B9 小时前
【02】MFC入门到精通——MFC 手动添加创建新的对话框模板
c++·mfc·mfc 手动添加创建新的对话框
linux kernel9 小时前
第七讲:C++中的string类
开发语言·c++