leetcode 333 最大BST子树
https://leetcode-cn.com/problems/largest-bst-subtree/
(https://www.nowcoder.com/questionTerminal/380d49d7f99242709ab4b91c36bf2acc)
#include <map>
#include <unordered_map>
#include <cstdio>
#include <vector>
using namespace std;
struct TreeNode
{
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int v_): val(v_), left(nullptr), right(nullptr){};
};
int maxcount = 0;
using node = pair<pair<bool, int>, pair<int,int>>;
// {以root为根子树是否是二叉搜索树,二叉搜索树的节点数,二叉搜索树的最大值,二叉搜索树的最小值}
node helper(TreeNode* root)
{
if(!root) return {{true, 0}, {INT32_MIN, INT32_MAX}};
auto left = helper(root->left);
auto right = helper(root->right);
node ret = {{false, 0}, {0, 0}};
if(left.first.first && right.first.first)
{
if(root->val > left.second.first && root->val < right.second.second)
{
ret.first.first = true;
ret.first.second = 1 + left.first.second + right.first.second;
ret.second.first = max(root->val, right.second.first);
ret.second.second = min(root->val, left.second.second);
}
}
if(ret.first.first) maxcount = max(maxcount, ret.first.second);
return ret;
}
int main()
{
unordered_map<int, TreeNode*> mp;
int n, rootval;
scanf("%d%d", &n, &rootval);
TreeNode* root = new TreeNode(rootval);
mp[rootval] = root;
for (int i = 0; i < n; ++i)
{
int fa, lch, rch;
scanf("%d%d%d", &fa, &lch, &rch);
TreeNode* r = nullptr, *lc = nullptr, *rc = nullptr;
if(!mp.count(fa))
{
r = new TreeNode(fa);
mp[fa] = r;
}
else r = mp[fa];
if(lch > 0)
{
if(!mp.count(lch))
{
lc = new TreeNode(lch);
mp[lch] = lc;
}
else lc = mp[lch];
}
if(rch > 0)
{
if(!mp.count(rch))
{
rc = new TreeNode(rch);
mp[rch] = rc;
}
else rc = mp[rch];
}
r->left = lc;
r->right = rc;
}
helper(root);
printf("%d\n", maxcount);
return 0;
}
class Solution {
private:
struct node
{
bool isBST;
int keysum;
int maxval;
int minval;
node():isBST(true), keysum(0), maxval(INT32_MIN), minval(INT32_MAX){};
node(bool b_, int k_, int max_, int min_):isBST(b_), keysum(k_), maxval(max_), minval(min_){};
};
int maxsum = 0;
node helper(TreeNode* root)
{
if(!root) return node();
auto left = helper(root->left);
auto right = helper(root->right);
node ret = node(false, root->val, INT32_MAX, INT32_MIN);
if(left.isBST && right.isBST)
{
if(root->val > left.maxval && root->val < right.minval)
{
ret.isBST = true;
ret.keysum += left.keysum + right.keysum;
ret.minval = min(root->val, left.minval);
ret.maxval = max(root->val, right.maxval);
}
}
if(ret.isBST) maxsum = max(maxsum, ret.keysum);
return ret;
}
public:
int maxSumBST(TreeNode* root) {
helper(root);
return maxsum;
}
};