#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using std::cout;
using std::cin;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution1 {
public:
void Tree(TreeNode* root, TreeNode** ret, int* n)
{
if (root == nullptr)
{
return;
}
Tree(root->left, ret, n);
ret[(*n)++] = root;
Tree(root->right, ret, n);
}
int TreeSize(TreeNode* root)
{
if (root == nullptr)
{
return 0;
}
int leftSize = TreeSize(root->left);
int rightSize = TreeSize(root->right);
int sum = leftSize + rightSize + 1;
return sum;
}
TreeNode* inorderSuccessor(TreeNode* root, TreeNode* p) {
int Size = TreeSize(root);
TreeNode** ret = new TreeNode * [Size];
int n = 0;
Tree(root, ret, &n);
TreeNode* next = NULL;
for (int i = 0; i < n; i++)
{
if (ret[i] == p && i < n - 1)
{
next = ret[i + 1];
}
}
return next;
}
};
int maxDepth(struct TreeNode* root) {
if (root == NULL)
{
return 0;
}
int treeleft = maxDepth(root->left);
int treeright = maxDepth(root->right);
return treeleft > treeright ? treeleft + 1 : treeright + 1;
}
bool isBalanced(struct TreeNode* root) {
if (root == NULL)
{
return true;
}
int treeleft = maxDepth(root->left);
int treeright = maxDepth(root->right);
return abs(treeleft - treeright) < 2 && isBalanced(root->left) && isBalanced(root->right);
}
struct Info3
{
int MaxDistance;
int height;
Info3(int a, int b)
{
MaxDistance = a;
height = b;
}
};
class Solution3 {
public:
Info3* Process3(TreeNode* head)
{
if (head == nullptr)
{
return new Info3(0, 0);
}
Info3* left = Process3(head->left);
Info3* right = Process3(head->right);
int height = fmax(left->MaxDistance, right->MaxDistance) + 1;
int Distance = fmax(left->height + right->height + 1, fmax(left->MaxDistance, right->MaxDistance));
delete left;
delete right;
return new Info3(Distance, height);
}
int MaxDistance(TreeNode* head)
{
return Process3(head)->MaxDistance;
}
};
struct Info4
{
bool isALLBST;
int max;
int min;
int Sum;
Info4(bool s, int _max, int _min, int _Sum)
{
isALLBST = s;
max = _max;
min = _min;
Sum = _Sum;
}
};
class Solution4
{
public:
Info4* Process4(TreeNode* head)
{
if (head == nullptr)
{
return nullptr;
}
Info4* left = Process4(head->left);
Info4* right = Process4(head->right);
int max = head->val;
int min = head->val;
if (left != nullptr)
{
max = fmax(max, left->max);
min = fmin(min, right->min);
}
if (right != nullptr)
{
max = fmax(max, right->max);
min = fmin(min, right->min);
}
bool s = false;
int sum = 0;
if (left != nullptr)
{
sum = left->Sum;
}
if (right != nullptr)
{
sum = right->Sum;
}
if (left == nullptr ? true : (left->isALLBST)
&& right == nullptr ? true : (right->isALLBST)
&& left == nullptr ? true : (left->max<head->val)
&& right == nullptr ? true : (right->min>head->val)
)
{
s = true;
sum = (left == nullptr ? 0 : left->Sum) + (right == nullptr ? 0 : right->Sum) + 1;
}
delete left;
delete right;
return new Info4(s, max, min, sum);
}
int MaxBstSize(TreeNode* head)
{
return Process4(head)->Sum;
}
};
struct Info5
{
int height;
int Sum;
Info5(int _height, int _Sum)
{
height = _height;
Sum = _Sum;
}
};
class Solution5
{
public:
Info5* Process5(TreeNode* head)
{
if (head == nullptr)
{
return new Info5(0, 0);
}
Info5* left = Process5(head->left);
Info5* right = Process5(head->right);
int height = fmax(left->height, right->height) + 1;
int Sum = left->Sum + right->Sum + 1;
delete left;
delete right;
return new Info5(height, Sum);
}
bool IsAllTree(TreeNode* head)
{
Info5* s = Process5(head);
bool f = pow(2, s->height) == s->Sum;
delete s;
return f;
}
};