当前位置:网站首页>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;
}
}
边栏推荐
- 【二叉树】二叉树中的伪回文路径
- 2021年数学建模B组代码
- Ukrainian officials: half of Ukrainian agricultural products are exported through the Danube port
- Add DNS server to LAN for domain name resolution
- From Web3 to web2.5, is it backward or another way?
- AimBetter洞察您的数据库,DPM 和 APM 解决方案
- What is time complexity
- DHCP and PPPoE protocols and packet capture analysis
- Overall introduction of Ruiji takeout project
- How to realize dynamic route switching and route caching in vuejs
猜你喜欢
随机推荐
HCIP(9)
tutorial/detailed_ workflow. Ipynb quantitative finance qlib Library
Part 8: creating camera classes
小程序 canvas 生成海报
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
hcip实验(14)
[binary tree] pseudo palindrome path in binary tree
HCIP(8)
HCIP(9)
vuejs中如何实现动态路由切换及路由的缓存
tutorial/detailed_workflow.ipynb 量化金融Qlib库
IFLYTEK written examination
【云原生之kubernetes】在kubernetes集群下的映射外部服务—Eendpoint
记录Flutter解决A RenderFlex overflowed by 7.3 pixels on the bottom溢出问题
Kubevera plug-in addons download address
TensorFlow Serving 高性能的机器学习模型服务系统
AimBetter洞察您的数据库,DPM 和 APM 解决方案
成立不到一年!MIT衍生量子计算公司完成900万美元融资
2021年数学建模B组代码
罗克韦尔AB PLC RSLogix数字量IO模块基本介绍








