当前位置:网站首页>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;
}
}
边栏推荐
- Starting from 1.5, build a micro Service Framework - call chain tracking traceid
- 小程序容器可以发挥的价值
- Cloud guide DNS, knowledge popularization and classroom notes
- 激动人心,2022开放原子全球开源峰会报名火热开启
- MCU通过UART实现OTA在线升级流程
- MYSQL GROUP_ The concat function realizes the content merging of the same ID
- cf:H. Maximal AND【位运算练习 + k次操作 + 最大And】
- Beginner redis
- Pointer - character pointer
- curlpost-php
猜你喜欢

Keepalive component cache does not take effect

Study diary: February 13, 2022

Intensive learning weekly, issue 52: depth cuprl, distspectrl & double deep q-network

The growth path of test / development programmers, the problem of thinking about the overall situation

esxi的安装和使用

Ffmpeg captures RTSP images for image analysis

Starting from 1.5, build a micro Service Framework - call chain tracking traceid

Set data real-time update during MDK debug

The relationship between FPGA internal hardware structure and code

I'm interested in watching Tiktok live beyond concert
随机推荐
C language programming (Chapter 6 functions)
95后CV工程师晒出工资单,狠补了这个,真香...
Problems and solutions of converting date into specified string in date class
A preliminary study of geojson
STM32 key chattering elimination - entry state machine thinking
cf:H. Maximal AND【位运算练习 + k次操作 + 最大And】
毕设-基于SSM高校学生社团管理系统
Free chat robot API
Common API classes and exception systems
STM32按键消抖——入门状态机思维
RAID disk redundancy queue
Novice entry depth learning | 3-6: optimizer optimizers
测试/开发程序员的成长路线,全局思考问题的问题......
Arduino六足机器人
Installation and use of esxi
免费的聊天机器人API
MYSQL GROUP_ The concat function realizes the content merging of the same ID
logstash清除sincedb_path上传记录,重传日志数据
Idea远程提交spark任务到yarn集群
Getting started with devkit