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);
    }
    
}
相关推荐
Tisfy1 小时前
LeetCode 3498.字符串的反转度:一次遍历
leetcode·字符串·题解·遍历
a187927218311 小时前
【算法】树(二):队列与祖先——BFS 骨架三配件、短路透传与合流
算法·leetcode·二叉树··bfs·算法讲解·树遍历
Navigator_Z5 小时前
LeetCode //C - 1254. Number of Closed Islands
c语言·算法·leetcode
0+1117 小时前
算法 --二分查找
c++·算法·leetcode
圣保罗的大教堂7 小时前
leetcode 1674. 使数组互补的最少操作次数 中等
leetcode
kukubuzai8 小时前
双指针系列二--末尾篇(3道题)
c++·算法·leetcode
圣保罗的大教堂1 天前
leetcode 835. 图像重叠 中等
leetcode
wabs6661 天前
关于二叉树【力扣572.另一棵树的子树的思考】
数据结构·c++·算法·leetcode·二叉树
hanlin031 天前
刷题笔记:力扣第41题-缺失的第一个正数
笔记·算法·leetcode
0+1111 天前
算法 --滑动窗口
c++·算法·leetcode