求二叉树叶子节点的个数
int getLeafNode(Node* root)
{
if(NULL == root)
return 0;
if(NULL == root->left && NULL == root->right)
return 1;
return getLeafNode(root->left) + getLeafNode(root->right);
}
求二叉树第k层的节点个数
int getLeafNode(Node* root,int k)
{
if(NULL == root)
return 0;
if(k==0)
return 1;
return getLeafNode(root->left,k-1) + getLeafNode(root->right,k-1);
}
一个数组中有一个数字的次数超过了数组的一半,求出这个字符
如:int a[]={2,3,2,2,2,2,2,5,4,1,2,3},求出超过一半的数字是2。
int nums(const vector<int>& v)
{
int tmp = v[0];
int count = 1;
for (int i = 1; i < v.size(); ++i)
{
if (count == 0)
{
tmp == v[i];
count++;
continue;
}
if (tmp == v[i])
++count;
else
--count;
}
return tmp;
}