力扣538. 把二叉搜索树转换为累加树

538. 把二叉搜索树转换为累加树 - 力扣(LeetCode)

右中左遍历

复制代码
/**
 * 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 num = 0;//全局变量用来记录上一次的和
    public TreeNode convertBST(TreeNode root) {
        if(root==null) return root;//递归终止条件
        convertBST(root.right);//右
        root.val = root.val + num;//中
        num = root.val;
        convertBST(root.left);//左
        return root;

    }
}
相关推荐
贾斯汀玛尔斯8 小时前
每天学一个算法--LSM-Tree(Log-Structured Merge Tree)
java·算法·lsm-tree
浅念-12 小时前
刷穿LeetCode:BFS 解决 Flood Fill 算法
数据结构·c++·算法·leetcode·职场和发展·bfs·宽度优先
做cv的小昊13 小时前
【TJU】研究生应用统计学课程笔记(8)——第四章 线性模型(4.1 一元线性回归分析)
笔记·线性代数·算法·数学建模·回归·线性回归·概率论
贾斯汀玛尔斯13 小时前
每天学一个算法--倒排索引(Inverted Index)
算法·inverted-index
小e说说13 小时前
打破偏科困境:这些学习软件助孩子重燃学习热情
算法
我命由我1234513 小时前
程序员的心理学学习笔记 - 空杯心态
经验分享·笔记·学习·职场和发展·求职招聘·职场发展·学习方法
月昤昽14 小时前
autoCAD二次开发 4.正多边形与collection区分
算法·c#·二次开发·autocad二次开发
休息一下接着来14 小时前
C++ 固定容量环形队列实现
c++·算法
im_AMBER14 小时前
手撕hot100之矩阵!看完这篇就AC~
javascript·数据结构·线性代数·算法·leetcode·矩阵