538. 把二叉搜索树转换为累加树

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 {
    int pre = 0;
    public TreeNode convertBST(TreeNode root) {
        convert(root);
        return root;
    }
    public void convert(TreeNode node){
        if(node==null) return;
        convertBST(node.right);
        node.val += pre;
        pre = node.val;
        convertBST(node.left);
    }
    
}
相关推荐
做怪小疯子1 小时前
LeetCode 热题 100——链表——相交链表
算法·leetcode·链表
while(努力):进步2 小时前
5G与物联网:连接万物的数字化未来
leetcode
2501_941804324 小时前
C++在高性能互联网服务开发与系统优化中的应用与实战经验解析
leetcode
希望有朝一日能如愿以偿5 小时前
力扣每日一题:可被三整除的最大和
数据结构·算法·leetcode
无敌最俊朗@6 小时前
力扣hot100-环形链表(2)142
算法·leetcode·链表
Elias不吃糖7 小时前
LeetCode每日一练(189, 122)
c++·算法·leetcode
小猪咪piggy7 小时前
【算法】day 19 leetcode 100 矩阵+贪心
算法·leetcode·矩阵
-森屿安年-7 小时前
LeetCode 11. 盛最多水的容器
开发语言·c++·算法·leetcode
2501_941803627 小时前
Go高性能分布式爬虫与Web数据采集实战分享:多线程抓取、反爬策略与性能优化经验
leetcode
flashlight_hi7 小时前
LeetCode 分类刷题:112. 路径总和
javascript·算法·leetcode