そこサブツリーが対称バイナリツリー、ミラー反転してバイナリツリー、および最大サブシーケンス、1か否かを判断するかどうかを - 1がn倍との間で発生します

ここに画像を挿入説明

/*
struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
	TreeNode(int x) :
			val(x), left(NULL), right(NULL) {
	}
};*/
class Solution {
public:
    bool func(TreeNode* pRoot1, TreeNode* pRoot2)
    {
        if(pRoot2 == nullptr)
            return true;
        if(pRoot1 == nullptr || pRoot2 == nullptr)
            return false;
        return pRoot1->val == pRoot2->val && func(pRoot1->left,pRoot2->left) && func(pRoot1->right,pRoot2->right);
    }
    bool HasSubtree(TreeNode* pRoot1, TreeNode* pRoot2)
    {
        if(pRoot1 == nullptr)
            return false;
        if(pRoot2 == nullptr)
            return false;
        if(pRoot1->val == pRoot2->val && func(pRoot1,pRoot2))
            return true;
        bool left = HasSubtree(pRoot1->left,pRoot2);
        if(left == true)
            return true;
        bool right = HasSubtree(pRoot1->right,pRoot2);
        return right;
    }
};

ここに画像を挿入説明

/*
struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
    TreeNode(int x) :
            val(x), left(NULL), right(NULL) {
    }
};
*/
class Solution {
public:
    bool func(TreeNode *pRoot1,TreeNode *pRoot2)
    {
        if(pRoot1 == nullptr && pRoot2 == nullptr)
            return true;
        if(pRoot1 == nullptr || pRoot2 == nullptr)
            return false;
        return pRoot1->val == pRoot2->val && func(pRoot1->left,pRoot2->right) && func(pRoot1->right,pRoot2->left);
    }
    bool isSymmetrical(TreeNode *pRoot) {
        if(pRoot == nullptr)
            return true;
        return func(pRoot->left,pRoot->right);
    }

};

ここに画像を挿入説明

/*
struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
	TreeNode(int x) :
			val(x), left(NULL), right(NULL) {
	}
};*/
class Solution {
public:
    void Mirror(TreeNode *pRoot) {

        if(pRoot == nullptr)
            return;
        if(pRoot->left == nullptr && pRoot->right == nullptr)
            return;
         
        TreeNode* pTemp = pRoot->left;
        pRoot->left = pRoot->right;
        pRoot->right = pTemp;
         
        if(pRoot->left != nullptr)
            Mirror(pRoot->left);
        if(pRoot->right != nullptr)
            Mirror(pRoot->right);
    }
};

ここに画像を挿入説明

class Solution {
public:
    int FindGreatestSumOfSubArray(vector<int> array) {
        int size = array.size();
        int maxnum = array[0];
        int count = array[0];
        for(int i = 1;i < size; ++i)
        {
            count += array[i];
            if(count <= 0)
                count = 0;
            if(maxnum < count)
                maxnum = count;
        }
        sort(array.begin(),array.end());   //这一块主要是为了判断特殊情况,数组里都是负数的情况
        if(array[size - 1] < 0)
            return array[size - 1];
        return maxnum;
    }
};

ここに画像を挿入説明

class Solution {
public:
    int NumberOf1Between1AndN_Solution(int n)
    {
        int ones = 0;
        for (long long m = 1; m <= n; m *= 10) 
        {
            int a = n/m, b = n%m;
            ones += (a + 8) / 10 * m + (a % 10 == 1) * (b + 1);
        }
        return ones;
    }
};

この質問は非常に明確ではありません
ここに画像を挿入説明

公開された230元の記事 ウォン称賛28 ビュー9317

おすすめ

転載: blog.csdn.net/weixin_43767691/article/details/103528814