首页 > 其他分享 >538. Convert BST to Greater Tree

538. Convert BST to Greater Tree

时间:2023-03-16 19:36:03浏览次数:39  
标签:node Convert Greater BST sum Tree convert

Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST.

 

//本解法是反中序遍历所有节点,即右中左的顺序

class Solution {     int sum = 0;     public TreeNode convertBST(TreeNode root) {        convert(root);        return root;     }     public void convert(TreeNode node) {        if (node == null) return;        convert(node.right);        node.val += sum;        sum = node.val;        convert(node.left);     } }

 

标签:node,Convert,Greater,BST,sum,Tree,convert
From: https://www.cnblogs.com/MarkLeeBYR/p/17223875.html

相关文章

  • BST中插入一个节点
     publicclassSolution{   publicTreeNodeinsertNode(TreeNoderoot,TreeNodenode){       //writeyourcodehere       if(root==null......
  • tkinter中treeview隔行显示不同的颜色
    隔行显示不同颜色的代码,这个牵涉到背景颜色,在3.8版的tkinter,要加多一些代码,才能让背景颜色起作用。这段要多加的代码就是:deffixed_map(option):return[elmforel......
  • list数组转tree树结构,并排序
    setTree(data){consttree=[];data.forEach(item=>{constchildren=this.list.filter(e=>e.parentId===item.id)if(children.length){......
  • C#封装FluentValidation,用了之后通篇还是AbstractValidator
    黑哥聊dotNetsharedonJun9,20223.8k10mins讲故事前几天看公司一个新项目使用了FluentValidation,大家都知道FluentValidation是一个非常强大的用于构建强类型验证规则的......
  • WPF TreeView 样式
    一、前言TreeView控件在项目中使用比较频繁,普通的TreeView并不能满足我们的需求。因此我们需要滴对TreeView进行改造。下面的内容将介绍仿QQ联系人TreeView样式及TreeView......
  • [CF1788F] XOR, Tree, and Queries
    题目描述Youaregivenatreeof$n$vertices.Theverticesarenumberedfrom$1$to$n$.Youwillneedtoassignaweighttoeachedge.Lettheweight......
  • delphi 再说TcxDBTreeList
    1.当我们绑定好数据库之后,默认是全部折叠的,只显示  +全部    cxDBTreeList1.Root.getFirstChild.Expand(False);//只展开第一层目录cxDBTreeList1.FullE......
  • [LeetCode] 958. Check Completeness of a Binary Tree
    Giventhe root ofabinarytree,determineifitisa completebinarytree.Ina completebinarytree,everylevel,exceptpossiblythelast,iscompletely......
  • postger平移oracle的function中涉及substr('float'*x,y,z)問題解決方案
    背景:工作中需要將oracle中的function平移到postger中,在一個view中調用了該function,function中需要對浮點數相乘操作,然後在截取,在AquaDataStudio中創建function能成功,但是......
  • Linux 工具命令(03): 使用 envsubst 渲染配置文件
    Linux工具命令(03):使用envsubst渲染配置文件如果在公众号文章发现状态为已更新,建议点击查看原文查看最新内容。状态:未更新原文链接:https://typonotes.co......