/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int preroot = 0;
    TreeNode* bstToGst(TreeNode* root) {
        if(root->right)
            bstToGst(root->right);
        preroot = root->val = root->val + preroot;
        if(root->left)
            bstToGst(root->left);
        return root;
    }
};

  

LeetCode -- 1038. Binary Search Tree to Greater Sum Tree的更多相关文章

  1. 【leetcode】1038. Binary Search Tree to Greater Sum Tree

    题目如下: Given the root of a binary search tree with distinct values, modify it so that every node has ...

  2. LeetCode: Validata Binary Search Tree

    LeetCode: Validata Binary Search Tree Given a binary tree, determine if it is a valid binary search ...

  3. LeetCode:Unique Binary Search Trees I II

    LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...

  4. Transform a BST to greater sum tree

    Given a BST, transform it into greater sum tree where each node contains sum of all nodes greater th ...

  5. [LeetCode] Validate Binary Search Tree 验证二叉搜索树

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  6. [LeetCode] Closest Binary Search Tree Value II 最近的二分搜索树的值之二

    Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...

  7. [LeetCode] Closest Binary Search Tree Value 最近的二分搜索树的值

    Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...

  8. [LeetCode] Recover Binary Search Tree 复原二叉搜索树

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  9. [LeetCode] Validate Binary Search Tree (两种解法)

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

随机推荐

  1. PHP array_flip()

    定义和用法 array_flip() 函数返回一个反转后的数组,如果同一值出现了多次,则最后一个键名将作为它的值,所有其他的键名都将丢失. 如果原数组中的值的数据类型不是字符串或整数,函数将报错. 语 ...

  2. Ejb in action(四)——购物车实例演示有状态会话Bean

    前面.我们介绍了一个入门实例.事实上那就是无状态回话Bean的经常使用情况. 上一篇文章中.我们介绍了无状态会话Bean和有状态会话Bean的相关概念.为了加深大家对它们的理解,我们一起来实现一个有状 ...

  3. MapReduce编程实战之“高级特性”

    本篇介绍MapReduce的一些高级特性,如计数器.数据集的排序和连接.计数器是一种收集作业统计信息的有效手段.排序是MapReduce的核心技术,MapReduce也可以运行大型数据集间的" ...

  4. js获取单独一个checkbox是否被选中

    <script language=javascript> function check(){ var xz=document.getElementById("xz"); ...

  5. CF 557A(Ilya and Diplomas-贪心)

    A. Ilya and Diplomas time limit per test 1 second memory limit per test 256 megabytes input standard ...

  6. @Validated数据校验

    //lastName必须是邮箱格式 @Email private String lastName;

  7. mongodb数据库的启动和停止

             数据库的启动和停止是数据库最主要的操作,也是数据库可以提供服务和被连接管理的前提条件.不同的数据库启动和停止的方式有一些差异.但也有同样之处,启动和关闭也必然会和数据库的进程有关 ...

  8. 学习笔记—— 一些UPDATE语句

    UPDATE语句原来还有许多种写法,有的还很复杂,孤陋寡闻的我甚至闻所未闻.幸甚至哉,记而志之. 0.UPDATE 表名 SET 字段... FROM ...的方式 USE AdventureWork ...

  9. poj--1753--Flip Game(dfs好题)

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37201   Accepted: 16201 Descr ...

  10. 解析HTML文件

    #!/usr/bin/env python3 # -*- coding: UTF-8 -*- from bs4 import BeautifulSoup import operator import ...