当前位置: 首页 > news >正文

230. Kth Smallest Element in a BST

Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.
Note:
You may assume k is always valid, 1 ≤ k ≤ BST's total elements.
Example 1:

Input: root = [3,1,4,null,2], k = 1
   3
  / \
 1   4
  \
   2
Output: 1

Example 2:

Input: root = [5,3,6,2,4,null,null,1], k = 3
       5
      / \
     3   6
    / \
   2   4
  /
 1
Output: 3

Follow up:
What if the BST is modified (insert/delete operations) often and you need to find the kth smallest frequently? How would you optimize the kthSmallest routine?

难度:medium

题目:给定二叉搜索树,找出其值第K小的结点。

思路:中序遍历

Runtime: 0 ms, faster than 100.00% of Java online submissions for Kth Smallest Element in a BST.
Memory Usage: 38.9 MB, less than 19.71% of Java online submissions for Kth Smallest Element in a BST.

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public int kthSmallest(TreeNode root, int k) {
        int[] result = {root.val};
        kthSmallest(root, k, new int[1], result);
        
        return result[0];
    }
    
    public void kthSmallest(TreeNode root, int k, int[] count, int[] result) {
        if (root == null || count[0] >= k) {
            return;
        }
        kthSmallest(root.left, k, count, result);
        count[0]++;
        if (count[0] == k) {
            result[0] = root.val;
            return;
        } 
        kthSmallest(root.right, k, count, result);
    }
}

相关文章:

  • vss使用笔记
  • 使用 Docker 部署 Spring Boot项目
  • luogu P1706全排列问题
  • 前端 CSS : 5# 纯 CSS 实现24小时超市
  • git
  • Mysql数据库的条件查询语句
  • 观《时间的朋友2017》总结
  • Angry Birds和广告系统泄露个人信息——FireEye对Angry Birds的分析
  • Vagrant (二) - 日常操作
  • 2017年终总结、随想
  • js
  • PHP multipart/form-data 远程DOS漏洞
  • java 内存分析 jmap
  • 功能架构图、信息结构图、产品结构图的区别和绘制方法
  • 我们雇佣了一只大猴子...
  • 345-反转字符串中的元音字母
  • C++回声服务器_9-epoll边缘触发模式版本服务器
  • css属性的继承、初识值、计算值、当前值、应用值
  • eclipse(luna)创建web工程
  • JAVA并发编程--1.基础概念
  • JS 面试题总结
  • Node + FFmpeg 实现Canvas动画导出视频
  • node和express搭建代理服务器(源码)
  • React系列之 Redux 架构模式
  • SAP云平台里Global Account和Sub Account的关系
  • vue数据传递--我有特殊的实现技巧
  • 创建一个Struts2项目maven 方式
  • 前嗅ForeSpider采集配置界面介绍
  • 如何胜任知名企业的商业数据分析师?
  • 软件开发学习的5大技巧,你知道吗?
  • 小李飞刀:SQL题目刷起来!
  • 原创:新手布局福音!微信小程序使用flex的一些基础样式属性(一)
  • 中文输入法与React文本输入框的问题与解决方案
  • No resource identifier found for attribute,RxJava之zip操作符
  • 3月27日云栖精选夜读 | 从 “城市大脑”实践,瞭望未来城市源起 ...
  • 积累各种好的链接
  • 交换综合实验一
  • ​Base64转换成图片,android studio build乱码,找不到okio.ByteString接腾讯人脸识别
  • ​LeetCode解法汇总1276. 不浪费原料的汉堡制作方案
  • ​LeetCode解法汇总2304. 网格中的最小路径代价
  • ​业务双活的数据切换思路设计(下)
  • ## 临床数据 两两比较 加显著性boxplot加显著性
  • #周末课堂# 【Linux + JVM + Mysql高级性能优化班】(火热报名中~~~)
  • (10)ATF MMU转换表
  • (39)STM32——FLASH闪存
  • (Note)C++中的继承方式
  • (附表设计)不是我吹!超级全面的权限系统设计方案面世了
  • (附源码)springboot码头作业管理系统 毕业设计 341654
  • (七)Knockout 创建自定义绑定
  • (转)【Hibernate总结系列】使用举例
  • (转)大型网站架构演变和知识体系
  • ***微信公众号支付+微信H5支付+微信扫码支付+小程序支付+APP微信支付解决方案总结...
  • .net反编译的九款神器
  • .NET文档生成工具ADB使用图文教程
  • /bin、/sbin、/usr/bin、/usr/sbin