打卡第九天,最后一天了。
1、阶乘后的零
int trailingZeroes(int n){
if(n==0){
return 0;
}
return n/5+trailingZeroes(n/5);
}
2、将数字变成0的操作次数
int numberOfSteps(int num){
if(num==0){
return 0;
}
if(num%2==1){
return numberOfSteps(num-1)+1;
}else{
return numberOfSteps(num/2)+1;
}
return 0;
}
3、完全二叉树的节点个数
int countNodes(struct TreeNode* root){
if(root==NULL){
return 0;
}
return countNodes(root->left)+countNodes(root->right)+1;
}
4、开幕式焰火
int hash[1024];
void transfer(struct TreeNode* root){
if(root){
hash[root->val]=1;
transfer(root->left);
transfer(root->right);
}
}
int numColor(struct TreeNode* root){
int i,sum=0;
memset(hash,0,sizeof(hash));
transfer(root);
for(i=1;i<=1000;++i){
if(hash[i]) ++sum;
}
return sum;
}
5、二叉搜索树的范围和
int rangeSumBST(struct TreeNode* root, int low, int high){
if(root==NULL){
return 0;
}else if(root->val>=low&&root->val<=high){
return root->val+rangeSumBST(root->left,low,high)+rangeSumBST(root->right,low,high);
}else if(root->val<low){
return rangeSumBST(root->right,low,high);
}else{
return rangeSumBST(root->left,low,high);
}
return 0;
}
6、二叉树的深度
int maxDepth(struct TreeNode* root){
if(root==NULL){
return 0;
}
int llen=maxDepth(root->left);
int rlen=maxDepth(root->right);
if(llen>=rlen){
return llen+1;
}else{
return rlen+1;
}
return 0;
}
7、二叉树的最大深度
int maxDepth(struct TreeNode* root){
if(root==NULL){
return 0;
}
int llen=maxDepth(root->left);
int rlen=maxDepth(root->right);
if(llen>=rlen){
return llen+1;
}else{
return rlen+1;
}
return 0;
}
8、翻转二叉树
struct TreeNode* invertTree(struct TreeNode* root){
if(root==NULL){
return 0;
}
struct TreeNode* left=invertTree(root->left);
struct TreeNode* right=invertTree(root->right);
root->left=right;
root->right=left;
return root;
}
//9、所有路径
//10、所有路径Ⅱ