求二叉树的节点总数及树的高度

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);
	}
}

你可能感兴趣的:(求二叉树的节点总数及树的高度)