https://www.cnblogs.com/grandyang/p/6591526.html

这个题本质上是中序遍历的反向。中序遍历是从小到大,而这个题目是从大到小,然后每个数加上比自己大的所有数的和。

因为实际上并没有改变树的结构,只是改变了原来树中节点的值,所以用void做返回值就可以了。

每次加sum实际上就是加的所有比当前节点大的数的和,并且这个和一定等于前一个节点的更新值。

class Solution {
public:
TreeNode* convertBST(TreeNode* root) {
int sum = ;
convertCore(root,sum);
return root;
}
void convertCore(TreeNode* root,int &sum){
if(root == NULL)
return;
convertCore(root->right,sum);
root->val += sum;
sum = root->val;
convertCore(root->left,sum);
}
};

leetcode538. Convert BST to Greater Tree的更多相关文章

  1. LN : leetcode 538 Convert BST to Greater Tree

    lc 538 Convert BST to Greater Tree 538 Convert BST to Greater Tree Given a Binary Search Tree (BST), ...

  2. 【leetcode_easy】538. Convert BST to Greater Tree

    problem 538. Convert BST to Greater Tree 参考 1. Leetcode_easy_538. Convert BST to Greater Tree; 完

  3. [Swift]LeetCode538. 把二叉搜索树转换为累加树 | Convert BST to Greater Tree

    Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original B ...

  4. LeetCode 538. Convert BST to Greater Tree (把二叉搜索树转换成较大树)

    Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original B ...

  5. 538. Convert BST to Greater Tree

    Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original B ...

  6. [LeetCode] Convert BST to Greater Tree 将二叉搜索树BST转为较大树

    Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original B ...

  7. [Leetcode]538. Convert BST to Greater Tree

    Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original B ...

  8. LeetCode 538 Convert BST to Greater Tree 解题报告

    题目要求 Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the origi ...

  9. Convert BST to Greater Tree

    Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original B ...

随机推荐

  1. windows server 2008 R2 开启远程桌面

    1.计算机  ——属性 2.远程设置 3.远程桌面-允许运行任意版本远程连接-选择用户-添加用户:Administrator;或是可以是其他非管理员用户(默认的系统管理员已经被授予了访问权限); 点确 ...

  2. WebForm 小项目【人员管理系统】分析

    简单的人员管理系统 展示页面 添加人员 --判断添加人员的各种条件限制 -- 各种提示 修改人员信息 -- 人员原来信息绑定 --密码不显示,密码不改时用原来密码 人员删除 using System; ...

  3. oracle与mysql(2)

    一.并发性 并发性是oltp数据库最重要的特性,但并发涉及到资源的获取.共享与锁定. mysql:mysql以表级锁为主,对资源锁定的粒度很大,如果一个session对一个表加锁时间过长,会让其他se ...

  4. LintCode Sqrt(x)

    Implement int sqrt(int x). Compute and return the square root of x. Have you met this question in a ...

  5. layui 图片上传+表单提交+ Spring MVC

    Layui 的上传是最常用的, 不可或缺, 记录一下代码, 以后复制都能用!! 1.前端HTML: <div class="layui-form-item"> < ...

  6. 【代码笔记】Web-ionic-头部与底部

    index代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> < ...

  7. Linux 下修改网卡MAC地址

    Linux下修改网卡MAC地址 by:授客 QQ:1033553122 例子:修改网卡接口eth0的mac地址 #停用网卡接口,比如eth0 # ifconfig eth0 down #编辑对应的网卡 ...

  8. IDEA报错: Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.datasource.url' in value "${spring.datasource.url}"

    运行审核流模块: 在ActivitiServiceApplication模块日志报错: Error starting ApplicationContext. To display the auto-c ...

  9. JMeter—前置处理器(九)

    参考<全栈性能测试修炼宝典JMeter实战>第六章 JMeter 元件详解中第四节前置处理器前置处理器用来处理请求前的一些准备工作,比如参数设置.环境变变量设置等 一.BeanShell ...

  10. 在ASP.NET Core中使用多环境

    原文地址:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/environments?view=aspnetcore-2.1#star ...