1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
largest-bst-subtree.cpp 1.39 KB
一键复制 编辑 原始数据 按行查看 历史
// Time: O(n)
// Space: O(h)
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int largestBSTSubtree(TreeNode* root) {
if (!root) {
return 0;
}
int max_size = 1;
largestBSTSubtreeHelper(root, &max_size);
return max_size;
}
private:
tuple<int, int, int> largestBSTSubtreeHelper(TreeNode* root, int *max_size) {
if (!root->left && !root->right) {
return make_tuple(1, root->val, root->val);
}
int left_size = 0, left_min = root->val, left_max = root->val;
if (root->left) {
tie(left_size, left_min, left_max) = largestBSTSubtreeHelper(root->left, max_size);
}
int right_size = 0, right_min = root->val, right_max = root->val;
if (root->right) {
tie(right_size, right_min, right_max) = largestBSTSubtreeHelper(root->right, max_size);
}
int size = 0;
if ((!root->left || left_size > 0) &&
(!root->right || right_size > 0) &&
left_max <= root->val && root->val <= right_min) {
size = 1 + left_size + right_size;
*max_size = max(*max_size, size);
}
return make_tuple(size, left_min, right_max);
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助