当前位置:网站首页>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;
}
}
边栏推荐
- Yolov5, pychar, Anaconda environment installation
- Leetcode 44 Wildcard matching (2022.02.13)
- Cannot resolve symbol error
- anconda下载+添加清华+tensorflow 安装+No module named ‘tensorflow‘+KernelRestarter: restart failed,内核重启失败
- Extension and application of timestamp
- Intensive learning weekly, issue 52: depth cuprl, distspectrl & double deep q-network
- cf:H. Maximal AND【位运算练习 + k次操作 + 最大And】
- 可恢复保险丝特性测试
- SAP Spartacus home 页面读取 product 数据的请求的 population 逻辑
- Analysis of the combination of small program technology advantages and industrial Internet
猜你喜欢
Arduino六足机器人
View class diagram in idea
Beginner redis
Data analysis thinking analysis methods and business knowledge - analysis methods (III)
cf:D. Insert a Progression【关于数组中的插入 + 绝对值的性质 + 贪心一头一尾最值】
KDD 2022 | EEG AI helps diagnose epilepsy
XML配置文件
Cannot resolve symbol error
I'm interested in watching Tiktok live beyond concert
Recoverable fuse characteristic test
随机推荐
免费的聊天机器人API
Kotlin core programming - algebraic data types and pattern matching (3)
【文件IO的简单实现】
[groovy] compile time meta programming (AST syntax tree conversion with annotations | define annotations and use groovyasttransformationclass to indicate ast conversion interface | ast conversion inte
curlpost-php
数据分析思维分析方法和业务知识——分析方法(三)
程序员成长第九篇:真实项目中的注意事项
Power query data format conversion, Split Merge extraction, delete duplicates, delete errors, transpose and reverse, perspective and reverse perspective
数据分析思维分析方法和业务知识——分析方法(二)
MIT博士论文 | 使用神经符号学习的鲁棒可靠智能系统
devkit入门
[simple implementation of file IO]
KDD 2022 | 脑电AI助力癫痫疾病诊断
Free chat robot API
可恢复保险丝特性测试
Leetcode:20220213 week race (less bugs, top 10% 555)
测试/开发程序员的成长路线,全局思考问题的问题......
synchronized 和 ReentrantLock
Leetcode Fibonacci sequence
The population logic of the request to read product data on the sap Spartacus home page