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);
    }
    
}
相关推荐
好易学·数据结构8 小时前
可视化图解算法56:岛屿数量
数据结构·算法·leetcode·力扣·回溯·牛客网
墨染点香10 小时前
LeetCode Hot100【5. 最长回文子串】
算法·leetcode·职场和发展
im_AMBER13 小时前
Leetcode 03 java
算法·leetcode·职场和发展
轮到我狗叫了13 小时前
力扣.1312让字符串成为回文串的最少插入次数力扣.105从前序和中序遍历构造二叉树牛客.拼三角力扣.57插入区间编辑
算法·leetcode·职场和发展
科大饭桶17 小时前
数据结构自学Day8: 堆的排序以及TopK问题
数据结构·c++·算法·leetcode·二叉树·c
木子.李34717 小时前
记录Leetcode中的报错问题
算法·leetcode·职场和发展
达文汐17 小时前
【中等】题解力扣22:括号生成
java·算法·leetcode·深度优先
Ylinnnnn1 天前
二分查找法
c++·学习·算法·leetcode·力扣·c·入门
达文汐1 天前
【中等】题解力扣21:合并两个有序链表
java·算法·leetcode·链表
qq_513970442 天前
力扣 hot100 Day46
算法·leetcode