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);
    }
    
}
相关推荐
努力学算法的蒟蒻2 小时前
day27(12.7)——leetcode面试经典150
算法·leetcode·面试
CoderYanger4 小时前
动态规划算法-子序列问题(数组中不连续的一段):28.摆动序列
java·算法·leetcode·动态规划·1024程序员节
有时间要学习4 小时前
面试150——第二周
数据结构·算法·leetcode
小白程序员成长日记8 小时前
2025.12.03 力扣每日一题
算法·leetcode·职场和发展
元亓亓亓8 小时前
LeetCode热题100--20. 有效的括号--简单
linux·算法·leetcode
熊猫_豆豆8 小时前
LeetCode 49.字母异位组合 C++解法
数据结构·算法·leetcode
小武~9 小时前
Leetcode 每日一题C 语言版 -- 234 basic calculator
linux·c语言·leetcode
小白程序员成长日记9 小时前
2025.12.02 力扣每日一题
数据结构·算法·leetcode
吃着火锅x唱着歌10 小时前
LeetCode 3583.统计特殊三元组
算法·leetcode·职场和发展
狐5711 小时前
2025-12-04-LeetCode刷题笔记-2211-统计道路上的碰撞次数
笔记·算法·leetcode