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);
    }
    
}
相关推荐
踩坑记录6 小时前
leetcode hot100 2.两数相加 链表 medium
leetcode·链表
历程里程碑8 小时前
滑动窗口---- 无重复字符的最长子串
java·数据结构·c++·python·算法·leetcode·django
TracyCoder12310 小时前
LeetCode Hot100(18/100)——160. 相交链表
算法·leetcode
放荡不羁的野指针11 小时前
leetcode150题-滑动窗口
数据结构·算法·leetcode
TracyCoder12312 小时前
LeetCode Hot100(13/100)——238. 除了自身以外数组的乘积
算法·leetcode
Anastasiozzzz12 小时前
LeetCode Hot100 215. 数组中的第K个最大元素
数据结构·算法·leetcode
让我上个超影吧12 小时前
【力扣76】最小覆盖子串
算法·leetcode·职场和发展
求梦82013 小时前
【力扣hot100题】合并两个有序链表(22)
算法·leetcode·链表
踩坑记录14 小时前
leetcode hot100 21.合并两个有序链表 链表 easy
leetcode
IT_Octopus14 小时前
力扣热题100 20. 有效的括号
算法·leetcode