当前位置:网站首页>Sword finger offer II 054. Sum of all values greater than or equal to nodes (medium binary search tree DFS)
Sword finger offer II 054. Sum of all values greater than or equal to nodes (medium binary search tree DFS)
2022-07-28 22:20:00 【Calm in the wind and rain】
The finger of the sword Offer II 054. Sum of all values greater than or equal to nodes
Given a binary search tree , Please replace the value of each node with the sum of all node values greater than or equal to the node value in the tree .
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 .
Example 1:

Input :root = [4,1,6,0,2,5,7,null,null,null,3,null,null,null,8]
Output :[30,36,21,36,35,26,15,null,null,null,33,null,null,null,8]
Example 2:
Input :root = [0,null,1]
Output :[1,null,1]
Example 3:
Input :root = [1,0,2]
Output :[3,3,2]
Example 4:
Input :root = [3,2,4,1]
Output :[7,9,4,10]
Tips :
The number of nodes in the tree is between 0 and 104 Between .
The value of each node is between -104 and 104 Between .
All the values in the tree Different from each other .
The given tree is a binary search tree .
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/w6cpku
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
analysis
utilize dfs Traversing the tree , The direction of traversal is Right -> in -> Left , Contrary to normal post order traversal , use sum Record the sum of all nodes starting from the node in the lower right corner , And re assign values to all nodes .
Answer key (Java)
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */
class Solution {
private int sum = 0;
public TreeNode convertBST(TreeNode root) {
if (root != null) {
convertBST(root.right);
sum += root.val;
root.val = sum;
convertBST(root.left);
}
return root;
}
}
边栏推荐
- Hcip seventh experiment
- Ruiji takeout project - development of business development function Day2
- Intranet penetration learning (III) horizontal movement of domain - planning tasks
- 04.toRef 默认值
- 2021数学建模B组练习
- Future trend of defi in bear market
- 使用百度EasyDL实现明厨亮灶厨师帽识别
- HCIP(8)
- Small program canvas generates posters
- HCIP(9)
猜你喜欢

HCIP(8)

HYDAC overflow valve db08a-01-c-n-500v

静态路由和缺省路由实验

2021 mathematical modeling group B exercise

Lin Xiaobin, head of Tencent cloud database, borrowed 100 million yuan to speculate in stocks? Insider: the amount is not true

使用百度EasyDL实现明厨亮灶厨师帽识别

40. Combined sum II

LCR测试仪最为主要的功能和用途都是什么

Bugku, Web: all filtered

Static route and default route experiment
随机推荐
Learning notes and summary of C language programming specification
Ruiji takeout project - development of business development function Day2
Future trend of defi in bear market
腾讯云数据库负责人林晓斌借一亿元炒股?知情人士:金额不实
SQL injection less42 (post stack injection)
Static route and default route experiment
What is time complexity
局域网添加DNS服务器进行域名解析
静态路由和缺省路由实验
[LiteratureReview]Object Detection and Mapping with Bounding Box Constraints
[machine learning] naive Bayesian classification of text -- Classification of people's names and countries
The difference between get and post
Mysql内置函数
深度学习必备:对数据集的拆分、根据拆分图片拆分labels、对全部标注标签进行区间检查
Bugku,Web:都过滤了
HCIP(14)
39. Combined sum
Add DNS server to LAN for domain name resolution
数据可视化新闻,不一样的新闻报道形式
Make trouble fishing day by day