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

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

}
相关推荐
智者知已应修善业19 小时前
【51单片机89C51及74LS273、74LS244组成】2022-5-28
c++·经验分享·笔记·算法·51单片机
Byron Loong1 天前
【c++】为什么有了dll和.h,还需要包含lib
java·开发语言·c++
坚果派·白晓明1 天前
【鸿蒙PC三方库移植适配框架解读系列】第一篇:Lycium C/C++ 三方库适配 — 概述与环境配置
c语言·开发语言·c++·harmonyos·开源鸿蒙·三方库·c/c++三方库
咩咦1 天前
C++学习笔记02:cin 和 cout 输入输出
c++·学习笔记·cin·输入输出·cout
咩咦1 天前
C++学习笔记05:引用和常引用
c++·学习笔记·引用·const·常引用
香蕉鼠片1 天前
算法过程中不会的
开发语言·c++
阿旭超级学得完1 天前
C++11包装器(function和bind)
java·开发语言·c++·算法·哈希算法·散列表
li星野1 天前
位运算 & 数学 & 高频进阶九题通关(Python + C++)
c++·python·学习·算法
磊 子1 天前
多态类原理+四种类型转换+异常处理
开发语言·c++·算法
王老师青少年编程1 天前
csp信奥赛C++高频考点专项训练之字符串 --【回文字符串】:回文拼接
c++·字符串·csp·高频考点·信奥赛·字符串回文·回文拼接