当前位置:网站首页>Convert binary search tree into cumulative tree (reverse middle order traversal)
Convert binary search tree into cumulative tree (reverse middle order traversal)
2022-07-06 00:51:00 【Hydrion-Qlz】
538. Convert binary search tree to accumulation tree
difficulty : secondary
Give a binary Search for Root node of tree , The node values of the tree are different , Please convert it to an accumulation tree (Greater Sum Tree), Make each node node The new value of is equal to or greater than in the original tree node.val The sum of the values of .
As a reminder , The binary search tree satisfies the following constraints :
- The left subtree of a node contains only the key Less than Node key node .
- The right subtree of a node contains only the key Greater than Node key node .
- Left and right subtrees must also be binary search trees .
** Be careful :** This topic and 1038: https://leetcode-cn.com/problems/binary-search-tree-to-greater-sum-tree/ identical

Ideas
I began to see that this problem was also a little confused , Thought of using middle order traversal , But it has never been possible to calculate , Took a look at the official solution , Referred to the Reverse middle order traversal , Well, I fixed my mind
If it is a positive mid order traversal , Then we traverse the elements from small to large
If it is reverse middle order traversal , Then our traversal order is from big to small
At this time, set a variable to count the current sum , When traversing an element , Add its value to sum in , And then sum Copy to node.val, Then go through the next element
package cn.edu.xjtu.carlWay.tree.BST2GreaterTree;
import cn.edu.xjtu.Util.TreeNode.TreeNode;
/** * 538. Convert binary search tree to accumulation tree * Give a binary Search for Root node of tree , The node values of the tree are different , Please convert it to an accumulation tree (Greater Sum Tree), Make each node node The new value of is equal to or greater than in the original tree node.val The sum of the values of . * <p> * As a reminder , The binary search tree satisfies the following constraints : * <p> * The left subtree of a node contains only the key Less than Node key node . * The right subtree of a node contains only the key Greater than Node key node . * Left and right subtrees must also be binary search trees . * Be careful : This topic and 1038: https://leetcode-cn.com/problems/binary-search-tree-to-greater-sum-tree/ identical * <p> * https://leetcode-cn.com/problems/convert-bst-to-greater-tree/ */
public class Solution {
int sum = 0;
public TreeNode convertBST(TreeNode root) {
if (root == null) {
return null;
}
convertBST(root.right);
sum += root.val;
root.val = sum;
convertBST(root.left);
return root;
}
}
边栏推荐
- NLP text processing: lemma [English] [put the deformation of various types of words into one form] [wet- > go; are- > be]
- Building core knowledge points
- I'm interested in watching Tiktok live beyond concert
- Anconda download + add Tsinghua +tensorflow installation +no module named 'tensorflow' +kernelrestart: restart failed, kernel restart failed
- 如何制作自己的機器人
- Reading notes of the beauty of programming
- FFmpeg抓取RTSP图像进行图像分析
- MYSQL GROUP_ The concat function realizes the content merging of the same ID
- SAP Spartacus home 页面读取 product 数据的请求的 population 逻辑
- How spark gets columns in dataframe --column, $, column, apply
猜你喜欢

Mobilenet series (5): use pytorch to build mobilenetv3 and learn and train based on migration

cf:H. Maximal AND【位运算练习 + k次操作 + 最大And】

uniapp开发,打包成H5部署到服务器

State mode design procedure: Heroes in the game can rest, defend, attack normally and attack skills according to different physical strength values.

【EI会议分享】2022年第三届智能制造与自动化前沿国际会议(CFIMA 2022)

Data analysis thinking analysis methods and business knowledge -- analysis methods (II)

Exciting, 2022 open atom global open source summit registration is hot

Set data real-time update during MDK debug

从 1.5 开始搭建一个微服务框架——调用链追踪 traceId

The third season of ape table school is about to launch, opening a new vision for developers under the wave of going to sea
随机推荐
ubantu 查看cudnn和cuda的版本
猿桌派第三季开播在即,打开出海浪潮下的开发者新视野
Anconda download + add Tsinghua +tensorflow installation +no module named 'tensorflow' +kernelrestart: restart failed, kernel restart failed
anconda下载+添加清华+tensorflow 安装+No module named ‘tensorflow‘+KernelRestarter: restart failed,内核重启失败
The relationship between FPGA internal hardware structure and code
How to make your own robot
Why can't mathematics give machine consciousness
Novice entry depth learning | 3-6: optimizer optimizers
MCU realizes OTA online upgrade process through UART
[groovy] JSON string deserialization (use jsonslurper to deserialize JSON strings | construct related classes according to the map set)
Idea remotely submits spark tasks to the yarn cluster
Calculate sha256 value of data or file based on crypto++
关于#数据库#的问题:(5)查询库存表中每本书的条码、位置和借阅的读者编号
Mobilenet series (5): use pytorch to build mobilenetv3 and learn and train based on migration
A preliminary study of geojson
Keepalive component cache does not take effect
The growth path of test / development programmers, the problem of thinking about the overall situation
《编程之美》读书笔记
After Luke zettlemoyer, head of meta AI Seattle research | trillion parameters, will the large model continue to grow?
Spark获取DataFrame中列的方式--col,$,column,apply