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

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

}
相关推荐
惺忪979817 分钟前
Qt C++11/14/17 新特性大全详解
开发语言·c++
Pacify_The_North22 分钟前
【C++11(二)】可变参数模板和 lambda表达式
java·开发语言·c++
顺顺 尼23 分钟前
包装器c++11
开发语言·c++
獭.獭.35 分钟前
C++ -- 二叉搜索树
数据结构·c++·算法·二叉搜索树
charlie1145141911 小时前
深入理解CC++的编译与链接技术8:Windows和Linux是如何搜寻动态库的?
c语言·c++·动态库·编译·编译技术
郝学胜-神的一滴1 小时前
Linux信号四要素详解:从理论到实践
linux·服务器·开发语言·网络·c++·程序人生
yangpipi-1 小时前
《C++并发编程实战》 第3章 在线程间共享数据
开发语言·c++
fish_xk1 小时前
c++基础
开发语言·c++
互亿无线明明1 小时前
如何为全球业务构建可扩展的“群发国际短信接口”?
java·c++·python·golang·eclipse·php·erlang
buyue__2 小时前
C++实现数据结构——链表
数据结构·c++·链表