[Leetcode-Tree] Kth Smallest Element in a BST

news/2024/7/3 5:52:29

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.

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?

Hint:

Try to utilize the property of a BST.
What if you could modify the BST node's structure?
The optimal runtime complexity is O(height of BST).

1.解题思路

本题需要找的是第k小的节点值,而二叉搜索树的中序遍历正好是数值从小到大排序的,那么这题就和中序遍历一个情况。

public class Solution {
    Stack<TreeNode> s=new Stack<TreeNode>();
    public int kthSmallest(TreeNode root, int k) {
        if(root==null) return 0;
        pushLeft(root);
        while(!s.empty()){
            TreeNode node=s.pop();
            if(--k==0) return node.val;
            if(node.right!=null)
                pushLeft(node.right);
        }
        return 0;
    }
    private void pushLeft(TreeNode root){
        TreeNode node=root;
        while(node!=null){
            s.push(node);
            node=node.left;
        }
    }
}

3.Follow up

如果树会经常被更改,为了效率,我们可以对树的构造稍作变更,添加一个属性,来标明该节点拥有的左子树的节点数,而这个Number就是比当前值小的节点个数,这样我们结合二分法,就很容易找到第k个小的节点值。


http://www.niftyadmin.cn/n/4115357.html

相关文章

如此的相似,不能-------Day84

生活中我们会遇到一些相似事儿&#xff0c;它可能是一个项目&#xff0c;我发现&#xff0c;你失去非常相似&#xff0c;其结果是不&#xff0c;它可以是人。你认为你一直在等待的是他&#xff08;她&#xff09;&#xff0c;终于可以找到&#xff0c;只需简单地认为。正是这样…

JS计算两个经纬度坐标与正北方向夹角

/** * 获取两个经纬度坐标正北方向夹角 * param {Array} o_latlngs 原点经纬度坐标 [经度, 纬度] * param {Array} latlngs 经纬度坐标 * return {Number} 返回角度 */ function getTwoPointAngle(o_latlngs, latlngs) { let A new MyLatLng(o_latlngs[0], o_latlngs[1]); le…

ubuntu 14.04 hadoop eclipse 0配置基本环境

动人的hadoop第二天。构造hadoop该环境还花了两天时间&#xff0c;在这里写自己配置的过程&#xff0c;我希望能帮助&#xff01; 我将文中用到的全部资源都分享到了 这里&#xff0c;点开就能下载&#xff0c;不须要一个个的找啦&#xff01; 当中有《Hadoop 技术内幕》这本书…

python gutter area / 设置断点、行号右边代码左边的空白栏

最后通过在设置里搜索 关键词&#xff1a;show Edito > General > Gutter Icons Show gutter icons转载于:https://www.cnblogs.com/IDRI/p/6082106.html

cesium绑定鼠标事件,及清除事件

1.绑定事件方法 说明&#xff1a; 方式一&#xff1a;方式一是每次都创建一个实例&#xff0c;可以多个共存且根据名字&#xff08;变量比如&#xff1a;下面的handler&#xff09;可以清除指定事件&#xff08;推荐使用&#xff09;。 方式二&#xff1a;方式二是直接在vie…

[logstash-input-log4j]插件使用详解

Log4j插件可以通过log4j.jar获取Java日志&#xff0c;搭配Log4j的SocketAppender和SocketHubAppender使用&#xff0c;常用于简单的集群日志汇总。 最小化的配置 input {log4j {host>"localhost"port>4560} } output {stdout {} } log4j插件配置host以及port就…

Maven项目中配置MyBatis Generator插件生成代码

2019独角兽企业重金招聘Python工程师标准>>> 参考&#xff1a; 官网&#xff1a;http://www.mybatis.org/generator/index.html 官网配置文件&#xff1a;http://www.mybatis.org/generator/configreference/xmlconfig.html 《 OSC-蛙牛的博客——数据库逆向框架代码…

cesium 获取多边形polygon中心点

//创建面 支持球面和平面坐标 var polygon viewer.entities.add({ polygon: { hierarchy: { positions: Cesium.Cartesian3.fromRadiansArray([ ]), }, outline: true, outlineColor: Cesium.Color.RED, outlineWidth: 3, material: Cesium.Color.YELLOW, }, }); var pyPosi…