//求一个数的平方根
int sqrt(int x)
{
if (x <= 0) return 0;
double c = x;
double old;
do
{
old = c;
c = (c + x / c) / 2.0;
} while (fabs(old - c) > 0.000001);
return int(c);
}
//二叉树深度计算
class BinaryTree {
public:
BinaryTree *lChild, *rChild;
int value;
};
//递归
int computeTreeDepth(BinaryTree* binaryTree) {
if (binaryTree == nullptr) {
return 0;
}
if (binaryTree->lChild == nullptr && binaryTree->rChild == nullptr) {
return 1;
}
int lDepth = computeTreeDepth(binaryTree->lChild) + 1;
int rDepth = computeTreeDepth(binaryTree->rChild) + 1;
return max(lDepth, rDepth);
}
//非递归
int computeTreeDepth(BinaryTree *binaryTree) {
//实例化队列
queue<BinaryTree*> mVisitList;
BinaryTree *tmp;
int depth = 0;
if (binaryTree == nullptr) {
//根为空,直接返回0
return 0;
}
//将根加入队列
mVisitList.push(binaryTree);
while (!mVisitList.empty()) {