C++学习笔记-7-递归函数

递归函数颇有意思,要讲一下。好比要计算n!咱们就这样定义:ios

#include <iostream>

using namespace std;


double factorial(double n)
{
    if(n==0)return 1;
    else
    {
        return n*factorial(n-1);
    }
}


int main()
{
    cout << "Hello world!" << endl;
    cout<<factorial(100)<<endl;
    return 0;
}

 

这个递归函数在二叉树的遍历中使用的最多,并且很方便:函数

template <class T>spa

void preorder(T *root){递归

if(root!==NULL){io

cout<<root->data<<"-->"class

preorder(root->leftleaf);stream

preorder(root->rightleaf);二叉树

}遍历

}co

相关文章
相关标签/搜索