230. 二叉搜索树中第K小的元素

题意

给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素。 你可以假设 k 总是有效的,1 ≤ k ≤ 二叉搜索树元素个数。

解题思路

  1. 中序遍历,利用Python3中提供的生成器方法;

  2. 中序遍历,判断存储结点值的数组是否到到k,则表明访问的一个结点就是第k个最小的元素;

  3. 先获取跟结点处于的位置(第几个最小的元素),如果它比k小,则从右子结点中找,如果它比k大,则从左子节点中找;

实现

class Solution:
   def kthSmallest(self, root: TreeNode, k: int) -> int:
     """
    利用了Python3中新增的生成器方法;
    """
       def gen(r):
           if r is not None:
               yield from gen(r.left)
               yield r.val
               yield from gen(r.right)
       
       it = gen(root)
       for _ in range(k):
           ans = next(it)
       return ans
     
def kthSmallest(self, root, k):
       """
      中序遍历,判断当前访问的结点是否是第k个最小的个数;
      :type root: TreeNode
      :type k: int
      :rtype: int
      """
       stack = []
       def helper(node):
           if not node:
               return
           helper(node.left)
           if len(stack) == k:
               return;
           stack.append(node.val)
           helper(node.right)
       
       if not root:
           return None

       helper(root)
       return stack[-1]

def kthSmallest(self, root, k):
       """
      :type root: TreeNode
      :type k: int
      :rtype: int
      """
       def helper(node):
           if not node:
               return 0
           # 获取以node为跟结点的结点总个数
           return helper(node.left) + helper(node.right) + 1
       
       if not root:
           return None

       while True:
           # number表示当前结点是第n小的元素
           number = helper(root.left) + 1
           
           # 每次判断当前结点处在第几小的位置
           # 如果比k大,说明值的范围比当前结点要小,则继续从当前结点的左子结点中找;
           # 如果比k小,说明值的范围比当前结点要大,则继续从当前结点的右子结点中找;
           if number < k:
               root = root.right
               k -= number
           elif number > k:
               root = root.left
           else:
               return root.val
       
       return 0

230. 二叉搜索树中第K小的元素的更多相关文章

  1. LeetCode 230. 二叉搜索树中第K小的元素(Kth Smallest Element in a BST)

    230. 二叉搜索树中第K小的元素 230. Kth Smallest Element in a BST 题目描述 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的 ...

  2. Java实现 LeetCode 230 二叉搜索树中第K小的元素

    230. 二叉搜索树中第K小的元素 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 说明: 你可以假设 k 总是有效的,1 ≤ k ≤ 二叉搜索树元素个数. ...

  3. [LeetCode]230. 二叉搜索树中第K小的元素(BST)(中序遍历)、530. 二叉搜索树的最小绝对差(BST)(中序遍历)

    题目230. 二叉搜索树中第K小的元素 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 题解 中序遍历BST,得到有序序列,返回有序序列的k-1号元素. 代 ...

  4. Leetcode:230. 二叉搜索树中第K小的元素

    Leetcode:230. 二叉搜索树中第K小的元素 Leetcode:230. 二叉搜索树中第K小的元素 思路: 利用BST的中序历遍的结果为其排序后的结果,我们可以利用其特性直接找到第k个中序遍历 ...

  5. 刷题-力扣-230. 二叉搜索树中第K小的元素

    230. 二叉搜索树中第K小的元素 题目链接 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/kth-smallest-element-in-a ...

  6. 【LeetCode】230#二叉搜索树中第K小的元素

    题目描述 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 说明: 你可以假设 k 总是有效的,1 ≤ k ≤ 二叉搜索树元素个数. 示例 1: 输入: ro ...

  7. LeetCode——230. 二叉搜索树中第K小的元素

    给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 说明: 你可以假设 k 总是有效的,1 ≤ k ≤ 二叉搜索树元素个数. 示例 1: 输入: root = ...

  8. leetcode 230 二叉搜索树中第K小的元素

    方法1:统计每个节点的子节点数目,当k>左子树节点数目时向左子树搜索,k=左子树节点数目时返回根节点,否则向右子树搜索. 方法2:递归中序遍历,这里开了O(n)空间的数组. class Solu ...

  9. 【LeetCode】230. 二叉搜索树中第K小的元素 Kth Smallest Element in a BST

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:算法题,刷题,Leetcode, 力扣,二叉搜索树,BST ...

随机推荐

  1. python 中is和== 的理解

    Python中的对象包含三要素:id.type.value其中id用来唯一标识一个对象,type标识对象的类型,value是对象的值is判断的是a对象是否就是b对象,是通过id来判断的==判断的是a对 ...

  2. .net下web页生产一维条形码

    code-39 前台 aspx <asp:Image ID="imgBandCode" runat="server" ImageUrl="~/W ...

  3. JavaScript中unicode编码与String互转(三种方法)

    1.引言 JS本身就支持unicode转string功能,一共有三种方式和String单个字符转unicode编码. 2.方法 //unicode转String 1. eval("'&quo ...

  4. [学习笔记]Javascript的包装对象

    例子1: var s="test"; s.len = 4; var t = s.len // t is undefined 原因是s是字符串,第二行代码,实际上是创建一个临时字符串 ...

  5. 实现div里的内容垂直居中

    ---恢复内容开始--- 在项目中我们会遇到这种情况,一个div的宽固定,里面的内容长度不定,不管是一行还是多行,都要垂直居中,有俩个实现方法: 1.使用absolute,top:50%,transf ...

  6. ORACLE与SQLSERVER数据转换

    前言: 将SQLServer数据库中的表和数据全量导入到Oracle数据库,通过Microsoft SqlServer Management Studio工具,直接导入到oracle数据库,免去了生成 ...

  7. Oracle 服务器结构

    [学习目标] 作为一个数据库管理员(DBA),经常会遇到各种没有见过的问题.除了宝贵的经验外, 通过理论基础去对问题进行判断.解决是至关重要的.因此,Oracle 服务器的结构和组成 是学习Oracl ...

  8. vue2进阶之v-model在组件上的使用

    v-model 用在 input 元素上时 v-model虽然很像使用了双向数据绑定的 Angular 的 ng-model,但是 Vue 是单项数据流,v-model 只是语法糖而已: <in ...

  9. java多线程快速入门(十二)

    在静态方法上面加synchonizd用的是字节码文件锁 package com.cppdy; class MyThread8 implements Runnable { private static ...

  10. 【hadoop】python通过hdfs模块读hdfs数据

    hdfs官网:http://hdfscli.readthedocs.io/en/latest/api.html 一个非常好的博客:http://blog.csdn.net/gamer_gyt/arti ...