/*
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;
}
};
这道题不是很明白