当前位置:网站首页>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(11)
- Lotus 1.16.0 extend sector expiration time
- [LiteratureReview]Object Detection and Mapping with Bounding Box Constraints
- array_ diff_ The method of not comparing array values when Assoc element is an array
- 行内元素和块级元素有什么区别?语义化作用
- [Ruiji takeout] day05 package management business development
- 【CVPR 2021】Cylinder3D:用于LiDAR点云分割的圆柱体非对称3D卷积网络
- HCIP(13)
- Small program canvas generates posters
猜你喜欢

Have you seen the management area decoupling architecture? Can help customers solve big problems

HCIP(11)

Record the fluent to solve the problem of a renderflex overflowed by 7.3 pixels on the bottom

40. Combined sum II

hcip实验(14)

JS DOM编程之平平无奇小练习

HCIP(11)

HCIP(15)

HCIP(8)

Day3 classification management of Ruiji takeout project
随机推荐
get和post的区别
Static route and default route experiment
什么是质因数,质因数(素因数或质因子)在数论里是指能整除给定正整数的质数
Day3 classification management of Ruiji takeout project
Basic introduction of Rockwell AB PLC rslogix digital quantity IO module
Necessary for in-depth learning: split the data set, split the labels according to the split pictures, and check the interval of all marked labels
From Web3 to web2.5, is it backward or another way?
HCIP(10)
Apifox: satisfy all your fantasies about API
openresty 请求鉴权
示波器发展史中的变化
HCIP第七次实验
静态路由和缺省路由实验
Record the fluent to solve the problem of a renderflex overflowed by 7.3 pixels on the bottom
hcip实验(15)
Make trouble fishing day by day
Aimbetter insight into your database, DPM and APM solutions
display 各值的区别
[LiteratureReview]Object Detection and Mapping with Bounding Box Constraints
Learn kotlin - extension function