当前位置:网站首页>Licking Exercise - 59 From Binary Search Trees to Greater Sum Trees
Licking Exercise - 59 From Binary Search Trees to Greater Sum Trees
2022-08-10 11:33:00 【qq_43403657】
59 从二叉搜索树到更大和树
1.问题描述
给出二叉 搜索 树的根节点,该二叉树的节点值各不相同,修改二叉树,使每个节点 node 的新值等于原树中大于或等于 node.val The sum of the values of all nodes of .
提醒一下,二叉搜索树满足下列约束条件:
节点的左子树仅包含键 小于 节点键的节点.
节点的右子树仅包含键 大于 节点键的节点.
左右子树也必须是二叉搜索树.
示例:
tree.png
输入:[4,1,6,0,2,5,7,null,null,null,3,null,null,null,8]
输出:[30,36,21,36,35,26,15,33,8]
可使用以下main函数:
#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;
}
void printTree(TreeNode* root) {
if (root == NULL) {
return;
}
bool isFirst=true;
queue<TreeNode*> q;
q.push(root);
while(!q.empty()) {
TreeNode* node = q.front();
q.pop();
if (node == NULL) {
continue;
}
if (!isFirst)
cout<<",";
cout<<node->val;
isFirst=false;
q.push(node->left);
q.push(node->right);
}
}
int main()
{
TreeNode* root;
root=inputTree();
TreeNode* res=Solution().bstToGst(root);
printTree(res);
}
2.输入说明
First enter the number of nodesn(注意,The nodes here include the ones in the titlenull空结点)
然后输入n个结点的数据,A node that needs to be filled with empty nodes,输入null.
3.输出说明
输出节点的信息,以逗号分隔
4.范例
输入
15
4 1 6 0 2 5 7 null null null 3 null null null 8
输出
30,36,21,36,35,26,15,33,8
5.代码
#include <iostream>
#include <queue>
#include <stack>
#include<cstdlib>
#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;
}
void printTree(TreeNode* root) {
if (root == NULL) {
return;
}
bool isFirst = true;
queue<TreeNode*> q;
q.push(root);
while (!q.empty()) {
TreeNode* node = q.front();
q.pop();
if (node == NULL) {
continue;
}
if (!isFirst)
cout << ",";
cout << node->val;
isFirst = false;
q.push(node->left);
q.push(node->right);
}
}
int sum = 0;
TreeNode* bstToGst(TreeNode *root)
{
//Reverse inorder traversal of a binary search tree
if (root != NULL)
{
bstToGst(root->right);//右子树
sum += root->val;//进行累加操作
root->val = sum;
bstToGst(root->left);//左子树
}
return root;
}
int main()
{
TreeNode* root;
root = inputTree();
TreeNode* res = bstToGst(root);
printTree(res);
}
边栏推荐
猜你喜欢
runtime-core.esm-bundler.js?d2dd:218 Uncaught TypeError: formRef.value?.validate is not a function
C#实战:基于ItextSharp技术标签生成小工具
从产品维度来看 我们为什么不能完全信任Layer2?
AutoCAD Map 3D功能之一暴力处理悬挂点(延伸)
推荐6个自媒体领域,轻松易上手
【勇敢饭饭,不怕刷题之链表】有序链表的合并
学长告诉我,大厂MySQL都是通过SSH连接的
mysql出现:ERROR 1524 (HY000): Plugin ‘123‘ is not loaded
ENVI 5.3软件安装包和安装教程
为什么Redis很快
随机推荐
快速上手,征服三种不同分布式架构调用方案
POJ 3101 Astronomy (Mathematics)
runtime-core.esm-bundler.js?d2dd:218 Uncaught TypeError: formRef.value?.validate is not a function
接口定义与实现
HDU 1520 Anniversary party (tree dp)
第5章相似矩阵及二次型(4)
不止跑路,拯救误操作rm -rf /*的小伙儿
开发模式对测试的影响
使用哈工大LTP测试分词并且增加自定义字典
rider内Mono脚本找不到引用资源
一文带你搞懂中断按键驱动程序之poll机制
AUTOCAD——减少样条曲线控制点数、CAD进阶练习(三)
Alibaba最新神作!耗时182天肝出来1015页分布式全栈手册太香了
用proteus直接仿真stm32-可以完全丢弃编程器
使用.NET简单实现一个Redis的高性能克隆版(六)
From the product dimension, why can't we fully trust Layer2?
【勇敢饭饭,不怕刷题之链表】链表反转的几种情况
Centos7环境使用Mysql离线安装包安装Mysql5.7
Double.doubleToLongBits()方法使用
电脑怎么设置屏幕息屏时间(日常使用分享)