力扣labuladong——一刷day36

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • 前言
  • [一、力扣230. 二叉搜索树中第K小的元素](#一、力扣230. 二叉搜索树中第K小的元素)
  • [二、力扣538. 把二叉搜索树转换为累加树](#二、力扣538. 把二叉搜索树转换为累加树)
  • [三、力扣1038. 从二叉搜索树到更大和树](#三、力扣1038. 从二叉搜索树到更大和树)

前言


首先,BST 的特性大家应该都很熟悉了: 1、对于 BST 的每一个节点 node,左子树节点的值都比 node 的值要小,右子树节点的值都比 node 的值大。 2、对于 BST 的每一个节点 node,它的左侧子树和右侧子树都是 BST。

一、力扣230. 二叉搜索树中第K小的元素

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 {
    List<Integer> list = new ArrayList<>();
    public int kthSmallest(TreeNode root, int k) {
        fun(root);
        return list.get(k-1);
    }
    public void fun(TreeNode root){
        if(root == null){
            return ;
        }
        fun(root.left);
        list.add(root.val);
        fun(root.right);
    }
}

不使用额外空间

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 res = 0,count = 0;
    public int kthSmallest(TreeNode root, int k) {
        fun(root,k);
        return res;
    }
    public void fun(TreeNode root,int k){
        if(root == null){
            return ;
        }
        fun(root.left,k);
        count ++;
        if(count == k){
            res = root.val;
            return ;
        }
        fun(root.right,k);
    }
}

二、力扣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 count = 0;
    public TreeNode convertBST(TreeNode root) {
        fun(root);
        return root;
    }
    public void fun(TreeNode root){
        if(root == null){
            return ;
        }
        fun(root.right);
        count += root.val;
        root.val = count;
        fun(root.left);
    }
}

三、力扣1038. 从二叉搜索树到更大和树

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 count = 0;
    public TreeNode bstToGst(TreeNode root) {
        fun(root);
        return root;
    }
    public void fun(TreeNode root){
        if(root == null){
            return;
        }
        fun(root.right);
        count += root.val;
        root.val = count;
        fun(root.left);
    }
}
相关推荐
若水不如远方5 小时前
Netty的四种零拷贝机制:深入原理与实战指南
java·netty
用户7493636848435 小时前
【开箱即用】一分钟使用java对接海外大模型gpt等对话模型,实现打字机效果
java
SimonKing5 小时前
一键开启!Spring Boot 的这些「魔法开关」@Enable*,你用对了吗?
java·后端·程序员
CoovallyAIHub5 小时前
英伟达ViPE重磅发布!解决3D感知难题,SLAM+深度学习完美融合(附带数据集下载地址)
深度学习·算法·计算机视觉
间彧6 小时前
Spring Boot集成Spring Security 6.x完整指南
java
xiezhr7 小时前
用户只需要知道「怎么办」,不需要知道「为什么炸了」
java·api·接口设计规范
xiezhr7 小时前
接口设计18条军规:写给那些半夜被“502”叫醒的人
java·api·restful
RainbowSea16 小时前
12. LangChain4j + 向量数据库操作详细说明
java·langchain·ai编程
RainbowSea16 小时前
11. LangChain4j + Tools(Function Calling)的使用详细说明
java·langchain·ai编程
考虑考虑20 小时前
Jpa使用union all
java·spring boot·后端