4-8 求二叉树高度 (20分)
2017-01-13 09:50
218 查看
本题要求给定二叉树的高度。
函数接口定义:
要求函数返回给定二叉树BT的高度值。
裁判测试程序样例:
函数接口定义:
int GetHeight( BinTree BT ); 其中BinTree结构定义如下: typedef struct TNode *Position; typedef Position BinTree; struct TNode{ ElementType Data; BinTree Left; BinTree Right; };
要求函数返回给定二叉树BT的高度值。
裁判测试程序样例:
#include <stdio.h> #include <stdlib.h> typedef char ElementType; typedef struct TNode *Position; typedef Position BinTree; struct TNode{ ElementType Data; BinTree Left; BinTree Right; }; BinTree CreatBinTree(); /* 实现细节忽略 */ int GetHeight( BinTree BT ); int main() { BinTree BT = CreatBinTree(); printf("%d\n", GetHeight(BT)); return 0; } /* 你的代码将被嵌在这里 */
我的解答
/* 用后序遍历递归求解 1)当前指针从root开始,如果当前指针为空,则深度为0 2)否则用其做左子递归,求其左子树的深度 3)用其右子树递归,求深度 4)当前树的深度为其左右子树的深度加1 */ int GetHeight( BinTree BT ) { int lDep,rDep; if(BT==NULL) { return 0;//1) } else { lDep=GetHeight(BT->Left); rDep=GetHeight(BT->Right); return (lDep>rDep?lDep:rDep)+1; } }
相关文章推荐
- 4-8 求二叉树高度 (20分)
- 4-8 求二叉树高度 (20分)
- PTA数据结构与算法题目集(中文)4-8 求二叉树高度 (20分)
- 二叉树非递归遍历、层次遍历、高度、节点数
- 二叉树的最小高度,最大高度(深度)和宽度
- (1)判断是否为完全二叉树 (2)求二叉树的高度
- 求二叉树的高度(深度+1)
- codevs1501 二叉树最大宽度和高度
- 数据结构_求二叉树的高度以及层次遍历二叉树算法_C语言源代码
- (016)给定一个有序数组(递增),写程序构建一棵具有最小高度的二叉树(keep it up)
- 二叉树的相关操作:创建、查找、求高度和深度、各种遍历(前、中、后、层序)等等
- 求二叉树的高度/销毁一颗二叉树【每日一题】
- 求二叉树高度(20 分)
- 每天一道LeetCode-----计算二叉树的最大深度及最小深度,判断二叉树是否是高度平衡二叉树
- 求二叉树的高度/销毁一颗二叉树-->Destroy(Node* root)
- 关于二叉树的宽度,高度,相等
- 树 二叉树最大宽度和高度
- 1019_计算二叉树的高度和结点数
- codevs 1501 二叉树最大宽度和高度
- CODE[VS]1501 二叉树最大宽度和高度