力扣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);
    }
}
相关推荐
艾莉丝努力练剑15 分钟前
【LeetCode&数据结构】单链表的应用——反转链表问题、链表的中间节点问题详解
c语言·开发语言·数据结构·学习·算法·leetcode·链表
武子康1 小时前
Java-72 深入浅出 RPC Dubbo 上手 生产者模块详解
java·spring boot·分布式·后端·rpc·dubbo·nio
_殊途2 小时前
《Java HashMap底层原理全解析(源码+性能+面试)》
java·数据结构·算法
椰椰椰耶3 小时前
【Spring】拦截器详解
java·后端·spring
没有bug.的程序员4 小时前
JAVA面试宝典 - 《MyBatis 进阶:插件开发与二级缓存》
java·面试·mybatis
没有羊的王K5 小时前
SSM框架学习——day1
java·学习
珊瑚里的鱼5 小时前
LeetCode 692题解 | 前K个高频单词
开发语言·c++·算法·leetcode·职场和发展·学习方法
又菜又爱coding5 小时前
安装Keycloak并启动服务(macOS)
java·keycloak
不知道叫什么呀6 小时前
【C】vector和array的区别
java·c语言·开发语言·aigc
wan_da_ren6 小时前
JVM监控及诊断工具-GUI篇
java·开发语言·jvm·后端