/**
 * 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. 解决ubuntu上opengl的问题

    装完ubuntu之后,对于opengl的程序总是出现问题,先将解决方案列出如下: http://www.linuxforums.org/forum/ubuntu-linux/175490-graphi ...

  2. Maven: java.lang.ClassNotFoundException: org.eclipse.aether.spi.connector.Transfer$State

    在mac中使用maven compile时发生以下错误: Maven: java.lang.ClassNotFoundException: org.eclipse.aether.spi.connect ...

  3. 飘逸的python - 实现glob style pattern

    一说起通配符,大家非常快就会想起*和? 号,有了通配符,使得表达能力大大增强,非常多linux命令都支持这个东西,事实上就是glob style pattern. 就连redis的keys命令都支持g ...

  4. 正则表达式,字符串中需要两个反斜杠“\\d”

    这个正则表达式为什么会有两个反斜杠? "^.*?\\.(jpg|png|bmp|gif)$"上面这个正则表达式为什么有两个反斜杠呢?反斜杠点\.就能表示点.了,为什么还要在\.前面 ...

  5. [LeetCode][Java] Minimum Depth of Binary Tree

    题目: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the ...

  6. CF # 296 C Glass Carving (并查集 或者 multiset)

    C. Glass Carving time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  7. 【HDOJ 5419】 Victor and Toys (排列组合)

    [HDOJ 5419] Victor and Toys n个玩具 m个区间 每一个玩具有一个beauty值 问任选三个区间 三区间的MINleft~MAXright的和的期望值 预处理一个数组 存放每 ...

  8. luogu2577 [ZJOI2005] 午餐 贪心

    题目大意 THU ACM小组的吃饭计划是这样的:先把所有的人分成两队,并安排好每队中各人的排列顺序,然后一号队伍到一号窗口去排队打饭,二号队伍到二号窗口去排队打饭.每个人打完饭后立刻开始吃,所有人都吃 ...

  9. 历届试题 邮局(dfs+剪枝)

      历届试题 邮局   时间限制:1.0s   内存限制:256.0MB      问题描述 C村住着n户村民,由于交通闭塞,C村的村民只能通过信件与外界交流.为了方便村民们发信,C村打算在C村建设k ...

  10. python关于文件的操作

    总是记不住API.昨晚写的时候用到了这些,但是没记住,于是就索性整理一下吧: python中对文件.文件夹(文件操作函数)的操作需要涉及到os模块和shutil模块. 得到当前工作目录,即当前Pyth ...