二叉树深度与宽度
2016-07-05 22:06
387 查看
#include "OJ.h"
#include <string.h>
#include <stdio.h>
/*
Description
给定一个二叉树,获取该二叉树的宽度深度。
Prototype
int GetBiNodeInfo(BiNode &head, unsigned int *pulWidth, unsigned int *pulHeight)
Input Param
head 需要获取深度的二叉树头结点
Output Param
pulWidth 宽度
pulHeight 高度
Return Value
0 成功
1 失败或其他异常
*/
int getwidth(BiNode *head)
{
if(head==NULL)
return 0;
if(head->left==NULL&&head->right==NULL)
return 1;
int wideleft=getwidth(head->left);
int wideright=getwidth(head->right);
return wideleft+wideright;
}
int getheight(BiNode *head)
{
if(head==NULL)//深度只有0
return 0;
int leftdep=getheight(head->left);
int rightdep=getheight(head->right);
return leftdep>rightdep?(leftdep+1):(rightdep+1);
}
int GetBiNodeInfo(BiNode &head, unsigned int *pulWidth, unsigned int *pulHeight)
{
/*在这里实现功能*/
if(!pulWidth||!pulHeight)
return -1;
*pulWidth=getwidth(&head);
*pulHeight=getheight(&head);
return 0;
}
#include <string.h>
#include <stdio.h>
/*
Description
给定一个二叉树,获取该二叉树的宽度深度。
Prototype
int GetBiNodeInfo(BiNode &head, unsigned int *pulWidth, unsigned int *pulHeight)
Input Param
head 需要获取深度的二叉树头结点
Output Param
pulWidth 宽度
pulHeight 高度
Return Value
0 成功
1 失败或其他异常
*/
int getwidth(BiNode *head)
{
if(head==NULL)
return 0;
if(head->left==NULL&&head->right==NULL)
return 1;
int wideleft=getwidth(head->left);
int wideright=getwidth(head->right);
return wideleft+wideright;
}
int getheight(BiNode *head)
{
if(head==NULL)//深度只有0
return 0;
int leftdep=getheight(head->left);
int rightdep=getheight(head->right);
return leftdep>rightdep?(leftdep+1):(rightdep+1);
}
int GetBiNodeInfo(BiNode &head, unsigned int *pulWidth, unsigned int *pulHeight)
{
/*在这里实现功能*/
if(!pulWidth||!pulHeight)
return -1;
*pulWidth=getwidth(&head);
*pulHeight=getheight(&head);
return 0;
}
相关文章推荐
- MySQL_5.5安装图解教程
- hdu 4901 The Romantic Hero
- 显示和隐藏元素display&&visibility
- 将Activity转成View添加到view中
- YII2 多语言包设置
- 线程池
- 改善SQL语句
- 利用js 判断浏览器类型
- 字符串分割
- Qt---多线程控制
- SCU2016-01 K找规律
- JDBC的数据库查询
- R 语言语法快速浏览
- ld: 6 duplicate symbols for architecture x86_64 clang: error: linker command的解决方法
- Python学习笔记7:Python字典和集合
- java 冒泡排序算法
- 第十一天打字
- 图算法5之1006
- 双链表基本操作
- Redis学习笔记(三)-数据类型之string类型