力扣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);
    }
}
相关推荐
e***749516 小时前
Spring Security 官网文档学习
java·学习·spring
n***i9516 小时前
Java NIO文件操作
java·开发语言·nio
星释17 小时前
Rust 练习册 72:多米诺骨牌与回溯算法
开发语言·算法·rust
笃行客从不躺平18 小时前
接口幂等性(Idempotency)
java
Hero | 柒18 小时前
JAVA反射机制
java·spring·反射
j***630818 小时前
Springboot项目中线程池使用整理
java·spring boot·后端
likuolei18 小时前
Eclipse 创建 Java 接口
java·数据库·eclipse
q***547518 小时前
Spring Boot 经典九设计模式全览
java·spring boot·设计模式
a***560618 小时前
Spring Boot接收参数的19种方式
java·spring boot·后端
福尔摩斯张18 小时前
Axios源码深度解析:前端请求库设计精髓
c语言·开发语言·前端·数据结构·游戏·排序算法