二叉树的叶子节点、第K层节点/数组中数字次数超过数组长度的一般

//一个数组中有一个数字的次数超过了数组的一半,求出这个字符。
//如:int a[] = { 2, 3, 2, 2, 2, 2, 2, 5, 4, 1, 2, 3 },求出超过一半的数字是2。
//我这里用来两种方法,第一种可以用哈希表来统计次数来解决这个问题。第二种是num出现的次数,出现一次num++,没有出现num--,当num减到0时,字符重新改变。因为出现的次数大于数组长度的一般,所以遍历完数组这个字符的num大于0int PassHalf(int* a, int len)
{
    if (a == NULL || len < 0)
    {
        return -1;
    }
    int i = 0;
    int num = 0;
    int key = 0;
    for (i = 0; i < len; i++)
    {
        if (num == 0)
        {
            key = a[i];
        }
        if (a[i] == key)
        {
            num++;
        }
        else
        {
            num--;
        }
    }
    return key;
}
//int PassHalf(int* a, int len)
//{
//  int i = 0;
//  int temp[1024] = { 0 };
//  for (i = 0; i < len; i++)
//  {
//      temp[a[i]]++;
//  }
//  for (i = 0; i < sizeof(temp)/sizeof(temp[0]); i++)
//  {
//      if (temp[i]>len / 2)
//      {
//          return i;
//      }
//  }
//}
int main()
{
    int a[] = { 2, 3, 2, 2, 2, 2, 2, 5, 4, 1, 2, 3 };
    int len = sizeof(a) / sizeof(a[0]);
    cout << PassHalf(a, len) << endl;
    system("pause");
    return 0;
}
//求二叉树叶子节点的个数 / 求二叉树第k层的节点个数。

/*struct BinaryNode
{
    int _val;
    BinaryNode* _left;
    BinaryNode* _right;
    BinaryNode(int x)
        :_val(x)
        , _left(NULL)
        , _right(NULL)
    {}
};
class Binarytree
{
public:
    typedef BinaryNode Node;
    Binarytree()
        :_root(NULL)
    {}
    Binarytree(int* a, int n, int invalid = int())//先构建左子树,再构建右子树
    {
        int index = 0;
        _root=_createtree(a, n, index, invalid);
    }
    ~Binarytree()
    {
        _Destroy(_root);
    }
    int LeafSize()//叶子节点的个数
    {
        int count = 0;
        _leafsize(_root,count);
        return count;
    }
    int GetLevel(int k)//第K层节点的个数
    {
        return _getlevel(_root,k);
    }
protected:
//根节点为第0int _getlevel(Node* root,int k)
    {
        if (root == NULL)
        {
            return 0;
        }
        if (k == 0)
        {
            return 1;
        }
        return _getlevel(root->_left, k - 1) + _getlevel(root->_right, k - 1);

    }
    //计算叶子节点的个数
    void _leafsize(Node* root,int& count)
    {
        if (root == NULL)
        {
            return ;
        }
        if (root->_left == NULL&&root->_right == NULL)//左右子树为空时count++
        {
            count++;
        }
        _leafsize(root->_left,count);//走左子树
        _leafsize(root->_right,count);//走右子树

    }
    //数组要往后走,所以这里index用引用
    Node* _createtree(int* a, int n, int& index, int invalid)
    {
        Node* root = NULL;
        if (index < n&&a[index] != invalid)
        {
            root = new Node(a[index]);
            root->_left = _createtree(a, n, ++index, invalid);
            root->_right = _createtree(a, n, ++index, invalid);

        }
        return root;
    }
    void _Destroy(Node* root)
    {
        if (root != NULL)
        {
            _Destroy(root->_left);
            _Destroy(root->_right);
            delete root;
        }
    }
protected:
    Node* _root;
};
int main()
{
    int a[10] = { 1, 2, 3, 0, 0, 4, 0, 0, 5, 6 };
    int len = sizeof(a) / sizeof(a[0]);
    Binarytree t(a, len, 0);
    cout << t.LeafSize() << endl;
    cout << t.GetLevel(0) << endl;
    system("pause");
    return 0;

}*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值