//二叉树节点的表示
struct Node{
int data;Node* leftChild;
Node* rightChild;
};
//返回二叉树的深度
int treeDepth(Node* root){if(root==NULL)
return 0;
else if(root->leftChild==NULL&&root->rightChild==NULL)
return 1;
else{
int ld=0,rd=0;
if(root->leftChild!=NULL)
ld=treeDepth(root->leftChild);
if(root->rightChild!=NULL)
rd=treeDepth(root->rightChild);
if(ld>rd) return ld+1;
else return rd+1;
}}
本文详细介绍了如何使用递归方法计算二叉树的深度,并通过实例代码演示了实现过程。
1785

被折叠的 条评论
为什么被折叠?



