struct BinaryTreeNode{
int data;
BinaryTreeNode *pLeft;
BinaryTreeNode *pRight;
};
//获取树中的节点数
int CountNode(BinaryTreeNode *pRoot){
if(pRoot == NULL)
return 0;
else{
return CountNode(pRoot->pLeft) + CountNode(pRoot->pRight) + 1;
}
}
//获取树的高度
int TreeDepth(BinaryTreeNode *pRoot){
if(pRoot == NULL)
return 0;
else{
int left = TreeDepth(pRoot->pLeft);
int right = TreeDepth(pRoot->pRight);
return left > right ? (left + 1) : (right + 1);
}
}
求二叉树的节点总数及树的高度
最新推荐文章于 2023-04-24 20:21:24 发布