当前位置:网站首页>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;
}
}
边栏推荐
- MCU realizes OTA online upgrade process through UART
- Kotlin core programming - algebraic data types and pattern matching (3)
- Cloud guide DNS, knowledge popularization and classroom notes
- Spark-SQL UDF函数
- Idea remotely submits spark tasks to the yarn cluster
- Intranet Security Learning (V) -- domain horizontal: SPN & RDP & Cobalt strike
- Spark DF增加一列
- Data analysis thinking analysis methods and business knowledge - analysis methods (III)
- 猿桌派第三季开播在即,打开出海浪潮下的开发者新视野
- STM32 key chattering elimination - entry state machine thinking
猜你喜欢

cf:C. The Third Problem【关于排列这件事】

MCU realizes OTA online upgrade process through UART

The third season of ape table school is about to launch, opening a new vision for developers under the wave of going to sea

Spark AQE

如何制作自己的機器人

Uniapp development, packaged as H5 and deployed to the server

Cf:c. the third problem

cf:D. Insert a Progression【关于数组中的插入 + 绝对值的性质 + 贪心一头一尾最值】

I'm interested in watching Tiktok live beyond concert

Cannot resolve symbol error
随机推荐
Power query data format conversion, Split Merge extraction, delete duplicates, delete errors, transpose and reverse, perspective and reverse perspective
MIT doctoral thesis | robust and reliable intelligent system using neural symbol learning
《强化学习周刊》第52期:Depth-CUPRL、DistSPECTRL & Double Deep Q-Network
Basic introduction and source code analysis of webrtc threads
【线上小工具】开发过程中会用到的线上小工具合集
Free chat robot API
[EI conference sharing] the Third International Conference on intelligent manufacturing and automation frontier in 2022 (cfima 2022)
golang mqtt/stomp/nats/amqp
C language programming (Chapter 6 functions)
Reading notes of the beauty of programming
如何制作自己的机器人
Illustrated network: the principle behind TCP three-time handshake, why can't two-time handshake?
[Online gadgets] a collection of online gadgets that will be used in the development process
OpenCV经典100题
Keepalive component cache does not take effect
MIT博士论文 | 使用神经符号学习的鲁棒可靠智能系统
Leetcode:20220213 week race (less bugs, top 10% 555)
Meta AI西雅图研究负责人Luke Zettlemoyer | 万亿参数后,大模型会持续增长吗?
What is the most suitable book for programmers to engage in open source?
State mode design procedure: Heroes in the game can rest, defend, attack normally and attack skills according to different physical strength values.