数据结构学习笔记(6)---二叉树的创建与求深度

来源:互联网 发布:剑三萝莉精致捏脸数据 编辑:程序博客网 时间:2024/06/11 01:38
对于二叉树的创建其实就是和先序遍历差不多,可以有三种创建二叉树的的方式,即先序创建吗,中序创建和后序创建,但是我一般都是按先序创建的,原因是比较简单容易理解,但是我对于后序与中序创建二叉树好像还没见到过,应该存在但上网查了一下还是,没有呀,这里就只写先序创建二叉树
void PreCreatBiTree(PBiTree *root){    int date;    cin >> date;    if (date ==  0)    {        *root = NULL;    }    else    {        *root = new BiTree;        (*root)->date = date;        PreCreatBiTree(&((*root)->lchild));        PreCreatBiTree(&((*root)->rchild));    }}
(2)求深度(也是递归思想)
int GetDeaph(PBiTree root){    int max, hRChild = 0, hLChild = 0;    if (root == NULL)    {        return 0;    }    else    {        hLChild = GetDeaph(root->lchild) + 1;        hRChild = GetDeaph(root->rchild) + 1;        max = hRChild > hLChild ? hRChild : hLChild;        return max;    }}
阅读全文
0 0