当前位置:网站首页>力扣练习——59 从二叉搜索树到更大和树
力扣练习——59 从二叉搜索树到更大和树
2022-08-10 10:59:00 【qq_43403657】
59 从二叉搜索树到更大和树
1.问题描述
给出二叉 搜索 树的根节点,该二叉树的节点值各不相同,修改二叉树,使每个节点 node 的新值等于原树中大于或等于 node.val 的所有节点的值之和。
提醒一下,二叉搜索树满足下列约束条件:
节点的左子树仅包含键 小于 节点键的节点。
节点的右子树仅包含键 大于 节点键的节点。
左右子树也必须是二叉搜索树。
示例:
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.输入说明
首先输入结点的数目n(注意,这里的结点包括题中的null空结点)
然后输入n个结点的数据,需要填充为空的结点,输入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)
{
//反向中序遍历二叉搜索树
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);
}
边栏推荐
猜你喜欢
Introduction to cross-end development of Taro applet
使用哈工大LTP测试分词并且增加自定义字典
中小规模网站架构
ISO9001在讲什么?过程方法和风险思维
Mobile and PC compatible loading and toast message plugins
OneFlow源码解析:算子指令在虚拟机中的执行
Redis设计与实现
STM32 encapsulation ESP8266 a key configuration function: implementations of AP mode and the STA mode switch, server and the client to create
From the product dimension, why can't we fully trust Layer2?
Open Office XML 格式里如何描述多段具有不同字体设置的段落
随机推荐
什么是抽象类
Gold, nine, silver and ten job-hopping seasons: technical interview questions and answers on Alibaba, Baidu, JD.com, and Meituan
[Brave food, not afraid of the linked list of brushing questions] Merging of ordered linked lists
JWT implements login authentication + Token automatic renewal scheme
开发模式对测试的影响
【勇敢饭饭,不怕刷题之链表】有序链表的合并
Emulate stm32 directly with proteus - the programmer can be completely discarded
OneFlow source code parsing: operator instructions executed in a virtual machine
[Brave food, not afraid to write the linked list] The problem of the penultimate node of the linked list
POJ 2891 Strange Way to Express Integers (Extended Euclidean)
POJ 1026 Cipher (置换群)
从脚本到剪辑,影像大师亲授的后期制作秘籍
十年架构五年生活-09 五年之约如期而至
是什么影响了MySQL性能?
Chapter 22 Source Code File REST API Reference (4)
越折腾越好用的 3 款开源 APP
关于振弦采集模块及采集仪振弦频率值准确率的问题
动作捕捉系统用于室内组合定位技术研究
Research on motion capture system for indoor combined positioning technology
程序员追求技术夯实基础学习路线建议