当前位置:网站首页>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;
}
}
边栏推荐
- The detailed page returns to the list and retains the original position of the scroll bar
- Location based mobile terminal network video exploration app system documents + foreign language translation and original text + guidance records (8 weeks) + PPT + review + project source code
- Novice entry depth learning | 3-6: optimizer optimizers
- 可恢复保险丝特性测试
- curlpost-php
- Spark SQL UDF function
- MCU realizes OTA online upgrade process through UART
- Calculate sha256 value of data or file based on crypto++
- Cf:c. the third problem
- Hundreds of lines of code to implement a JSON parser
猜你喜欢
Data analysis thinking analysis methods and business knowledge -- analysis methods (II)
[groovy] XML serialization (use markupbuilder to generate XML data | create sub tags under tag closures | use markupbuilderhelper to add XML comments)
2022-02-13 work record -- PHP parsing rich text
FFmpeg抓取RTSP图像进行图像分析
看抖音直播Beyond演唱会有感
猿桌派第三季开播在即,打开出海浪潮下的开发者新视野
Building core knowledge points
XML配置文件
vSphere实现虚拟机迁移
Idea remotely submits spark tasks to the yarn cluster
随机推荐
Spark SQL UDF function
Zhuhai laboratory ventilation system construction and installation instructions
Study diary: February 13, 2022
Leetcode:20220213 week race (less bugs, top 10% 555)
State mode design procedure: Heroes in the game can rest, defend, attack normally and attack skills according to different physical strength values.
Fibonacci number
Basic introduction and source code analysis of webrtc threads
Anconda download + add Tsinghua +tensorflow installation +no module named 'tensorflow' +kernelrestart: restart failed, kernel restart failed
Spark AQE
[Online gadgets] a collection of online gadgets that will be used in the development process
VSphere implements virtual machine migration
XML Configuration File
The detailed page returns to the list and retains the original position of the scroll bar
Uniapp development, packaged as H5 and deployed to the server
[groovy] JSON string deserialization (use jsonslurper to deserialize JSON strings | construct related classes according to the map set)
程序员成长第九篇:真实项目中的注意事项
Yolov5、Pycharm、Anaconda环境安装
A preliminary study of geojson
XML配置文件
Calculate sha256 value of data or file based on crypto++