当前位置:网站首页>Licking Exercise - 60 Maximum key-value sum of binary search subtrees
Licking Exercise - 60 Maximum key-value sum of binary search subtrees
2022-08-10 11:34:00 【qq_43403657】
60 二叉搜索子树的最大键值和
1.问题描述
给你一棵以 root 为根的 二叉树 (注意:不一定是二叉搜索树),Would you please return any binary search subtree of the biggest keys and.
二叉搜索树的定义如下:
任意节点的左子树中的键值都 小于 此节点的键值.
任意节点的右子树中的键值都 大于 此节点的键值.
任意节点的左子树和右子树都是二叉搜索树.
示例 1:
输入:root = [1,4,3,2,4,2,5,null,null,null,null,null,null,4,6]
输出:20
解释:因为以1For the root of binary tree is not a binary search tree,So the key value is zero 3 的子树是和最大的二叉搜索树.
示例 2:
输入:root = [4,3,null,1,2]
输出:2
解释:因为以3或4For the root of binary tree is not a binary search tree,So the key value is zero 2 的单节点子树是和最大的二叉搜索树.
示例 3:
输入:root = [-4,-2,-5]
输出:0
解释:所有节点键值都为负数,和最大的二叉搜索树为空.
示例 4:
输入:root = [2,1,3]
输出:6
示例 5:
输入:root = [5,4,8,3,null,6,3]
输出:7
说明:
每棵树最多有 20000 个节点.
每个节点的键值在 [-10^4 , 10^4] 之间.
可使用以下main函数:
#include
#include
#include
#include
#include
#include
#include
using namespace std;
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(NULL), right(NULL) {}
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
TreeNode* inputTree()
{
int n,count=0;
char item[100];
cin>>n;
if (n==0)
return NULL;
cin>>item;
TreeNode* root = new TreeNode(atoi(item));
count++;
queue<TreeNode*> nodeQueue;
nodeQueue.push(root);
while (count<n)
{
TreeNode* node = nodeQueue.front();
nodeQueue.pop();
cin>>item;
count++;
if (strcmp(item,"null")!=0)
{
int leftNumber = atoi(item);
node->left = new TreeNode(leftNumber);
nodeQueue.push(node->left);
}
if (count==n)
break;
cin>>item;
count++;
if (strcmp(item,"null")!=0)
{
int rightNumber = atoi(item);
node->right = new TreeNode(rightNumber);
nodeQueue.push(node->right);
}
}
return root;
}
int main()
{
TreeNode* root;
root=inputTree();
int res=Solution().maxSumBST(root);
cout<<res;
}
2.输入说明
首先输入结点的数目n(注意,这里的结点包括题中的null空结点,所以这里的n可能超过20000)
然后输入n个结点的数据,需要填充为空的结点,输入null.
3.输出说明
输出一个整数
4.范例
输入
15
1 4 3 2 4 2 5 null null null null null null 4 6
输出
20
5.代码
#include <iostream>
#include <queue>
#include <stack>
#include<cstdlib>
#include <climits>
#include <cstring>
#include<map>
using namespace std;
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(NULL), right(NULL) {
}
TreeNode(int x) : val(x), left(NULL), right(NULL) {
}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {
}
};
TreeNode* inputTree()
{
int n, count = 0;
char item[100];
cin >> n;
if (n == 0)
return NULL;
cin >> item;
TreeNode* root = new TreeNode(atoi(item));
count++;
queue<TreeNode*> nodeQueue;
nodeQueue.push(root);
while (count < n)
{
TreeNode* node = nodeQueue.front();
nodeQueue.pop();
cin >> item;
count++;
if (strcmp(item, "null") != 0)
{
int leftNumber = atoi(item);
node->left = new TreeNode(leftNumber);
nodeQueue.push(node->left);
}
if (count == n)
break;
cin >> item;
count++;
if (strcmp(item, "null") != 0)
{
int rightNumber = atoi(item);
node->right = new TreeNode(rightNumber);
nodeQueue.push(node->right);
}
}
return root;
}
int res = INT_MIN;
vector<int> dfs(TreeNode* node) {
if (node==NULL) //Note here in front of the two values cannot write back
return {
INT_MAX, INT_MIN, 0, true };//Four elements are respectively the minimum ,最大值 ,总和 ,是否为搜索二叉树
auto leftVec = dfs(node->left);//遍历左子树
auto rightVec = dfs(node->right);//遍历右子树
// Update the current subtree and
int sum = leftVec[2] + rightVec[2] + node->val;
bool isBitree = false;
// Is greater than the left subtree maximum,That is larger than left subtree all elements
// 小于右子树最小值,Than right subtree all elements are small
if (node->val > leftVec[1] && node->val < rightVec[0]) {
// The left tree and right subtree is binary search tree
if (leftVec[3] && rightVec[3]) {
isBitree = true;
}
}
if (isBitree) res = max(res, sum);
// Update the current subtree of the maximum and the minimum
int maxVal = max(max(leftVec[1], rightVec[1]), node->val);
int minVal = min(min(leftVec[0], rightVec[0]), node->val);
return {
minVal, maxVal, sum, isBitree };//返回
}
int maxSumBST(TreeNode *root)
{
dfs(root);
return res > 0 ? res : 0;
}
int main()
{
TreeNode* root;
root = inputTree();
int res = maxSumBST(root);
cout << res;
}
边栏推荐
- 电脑怎么设置屏幕息屏时间(日常使用分享)
- 做自媒体月入几万?博主们都在用的几个自媒体工具
- Hangdian Multi-School-Loop-(uncertainty greedy + line segment tree)
- GPU accelerated Pinterest recommendation model, the number of parameters increased by 100 times, and the user activity increased by 16%
- 蔚来-软件开发工程师一面记录
- 力扣练习——59 从二叉搜索树到更大和树
- 微信小程序,全局变量一个地方改变了其他地方的状态也跟着改变。
- 使用哈工大LTP测试分词并且增加自定义字典
- Research on motion capture system for indoor combined positioning technology
- The brave rice rice, does not fear the brush list of 】 list has a ring
猜你喜欢

Unsafe的一些使用技巧

MLX90640 红外热成像仪测温传感器 手机 APP 软件 RedEye 连接详细

std::move()

mysql appears: ERROR 1524 (HY000): Plugin '123' is not loaded

自媒体爆款标题怎么写?手把手教你写热门标题

Article take you understand interrupt the key driver of polling mechanism

从产品维度来看 我们为什么不能完全信任Layer2?

Pycharm终端出现PS问题、conda或activate不是内部命令问题..

为什么Redis很快

LAXCUS分布式操作系统安全管理
随机推荐
面试官:项目中 Dao、Service、Controller、Util、Model 怎么划分的?
LeetCode_152_乘积最大子数组
1-IMU参数解析以及选择
4 of huawei offer levels, incredibly side is easing the bit in the interview ali?
In August the DB list latest scores - database Engines
OneFlow source code parsing: operator instructions executed in a virtual machine
Introduction to Software Architecture
【勇敢饭饭,不怕刷题之链表】链表倒数节点问题
力扣练习——56 寻找右区间
3款不同类型的自媒体免费工具,有效提高创作、运营效率
LAXCUS分布式操作系统安全管理
力扣练习——60 二叉搜索子树的最大键值和
STM32 encapsulation ESP8266 a key configuration function: implementations of AP mode and the STA mode switch, server and the client to create
flask-restplus接口地址404问题
Where can I view the version record of WeChat applet submission review history?
【机器学习】浅谈正规方程法&梯度下降
力扣练习——64 最长和谐子序列
越折腾越好用的 3 款开源 APP
CPU多级缓存与缓存一致性
What is an abstract class