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);
    }
    
}
相关推荐
bbq粉刷匠26 分钟前
力扣--两数之和(Java)
java·leetcode
树在风中摇曳26 分钟前
LeetCode 1658 | 将 x 减到 0 的最小操作数(C语言滑动窗口解法)
c语言·算法·leetcode
.柒宇.1 小时前
力扣hoT100之找到字符串中所有字母异位词(java版)
java·数据结构·算法·leetcode
YoungHong19922 小时前
面试经典150题[063]:删除链表的倒数第 N 个结点(LeetCode 19)
leetcode·链表·面试
青山的青衫3 小时前
【前后缀】Leetcode hot 100
java·算法·leetcode
啊吧怪不啊吧4 小时前
二分查找算法介绍及使用
数据结构·算法·leetcode
Kuo-Teng13 小时前
LeetCode 160: Intersection of Two Linked Lists
java·算法·leetcode·职场和发展
橘颂TA16 小时前
【剑斩OFFER】算法的暴力美学——点名
数据结构·算法·leetcode·c/c++
愚润求学19 小时前
【动态规划】专题完结,题单汇总
算法·leetcode·动态规划
·白小白21 小时前
力扣(LeetCode) ——43.字符串相乘(C++)
c++·leetcode