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

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

}
相关推荐
Stanford_11063 小时前
如何利用Python进行数据分析与可视化的具体操作指南
开发语言·c++·python·微信小程序·微信公众平台·twitter·微信开放平台
千里马-horse4 小时前
Async++ 源码分析8--partitioner.h
开发语言·c++·async++·partitioner
Lucis__5 小时前
再探类&对象——C++入门进阶
开发语言·c++
北京不会遇到西雅图6 小时前
【SLAM】【后端优化】不同优化方法对比
c++·机器人
jndingxin6 小时前
c++多线程(6)------ 条件变量
开发语言·c++
程序员莫小特7 小时前
老题新解|大整数加法
数据结构·c++·算法
洲覆8 小时前
C++ 模板、泛型与 auto 关键字
开发语言·数据结构·c++
千里马-horse9 小时前
Async++ 源码分析7--parallel_reduce.h
开发语言·c++·async++·parallel_reduce
江公望9 小时前
Qt QThread使用方法入门浅解
c++·qt
叫我龙翔9 小时前
【MySQL】从零开始了解数据库开发 --- 数据表的约束
android·c++·mysql·数据库开发