1、二叉树定义
typedef struct BTreeNodeElement_t_ {
void *data;
} BTreeNodeElement_t;
typedef struct BTreeNode_t_ {
BTreeNodeElement_t *m_pElemt;
struct BTreeNode_t_ *m_pLeft;
struct BTreeNode_t_ *m_pRight;
} BTreeNode_t;
2、求二叉树深度
定义:对任意一个子树的根节点来说,它的深度=左右子树深度的最大值+1
(1)递归实现
如果根节点为NULL,则深度为0
如果根节点不为NULL,则深度=左右子树的深度的最大值+1
int GetBTreeDepth( BTreeNode_t *pRoot)
{
if( pRoot == NULL )
return 0;
int lDepth = GetBTreeDepth( pRoot->m_pLeft);
int rDepth = GetBTreeDepth( pRoot->m_pRight);
return ((( lDepth > rDepth )? lDepth: rDepth) + 1 );
}
原文:http://blog.csdn.net/beitiandijun/article/details/41930583