230. Kth Smallest Element in a BSTs

题目大意:

给定一个二叉搜索树,找出第k个最小的元素。

解题思路:

根据二叉搜索树的特性,左子树小于根节点小于右子树。我们可以对二叉树进行中序遍历(Inorder Traverse)并将元素保存在一个ArrayList当中。
第k个最小的元素也就是在ArrayList中index为k-1的元素
中序遍历:left -> root -> right

复杂度:

Time Complexity: O(N)
Space Complexity: O(N)

Code示例:

/*** 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 {public int kthSmallest(TreeNode root, int k) {List<Integer> nums = inorder(root, new ArrayList<>());return nums.get(k-1);}public List<Integer> inorder (TreeNode root, List<Integer> list) {if (root == null) return list;inorder(root.left, list);list.add(root.val);inorder(root.right, list);return list;}
}

230. Kth Smallest Element in a BSTs相关推荐

  1. [leetcode] 230. Kth Smallest Element in a BST 找出二叉搜索树中的第k小的元素

    题目大意 https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/ 230. Kth Smallest Elem ...

  2. LeetCode 230. Kth Smallest Element in a BST--C++,Python解法--面试真题--找二叉树中第K小的元素

    题目地址:Kth Smallest Element in a BST - LeetCode Given a binary search tree, write a function kthSmalle ...

  3. 230. Kth Smallest Element in a BST ——迭代本质:a=xx1 while some_condition: a=xx2

    Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...

  4. Leetcode - 230. Kth Smallest Element in a BST (BST)

    Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...

  5. 230. Kth Smallest Element in a BST

    Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...

  6. 230. Kth Smallest Element in a BST

    题目: Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. ...

  7. LeetCode 230. Kth Smallest Element in a BST

    Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...

  8. leetcode 230. Kth Smallest Element in a BST | 230. 二叉搜索树中第K小的元素(Java)

    题目 https://leetcode.com/problems/kth-smallest-element-in-a-bst/ 题解 方法1:中序遍历+剪枝 import java.util.Arra ...

  9. [LeetCode] 230. Kth Smallest Element in a BST

    题目内容 https://leetcode-cn.com/problems/kth-smallest-element-in-a-bst/ 给定一个二叉搜索树,编写一个函数 kthSmallest 来查 ...

  10. [LeetCode] Kth Smallest Element in a BST

    Kth Smallest Element in a BST Given a binary search tree, write a function kthSmallest to find the k ...

最新文章

  1. NYOJ 536 开心的mdd
  2. 《LED调光-DMX512灯光协义接收控制》转
  3. LinkedList常用方法笔记
  4. Vue:安装Vue Devtools调试工具简便方法解决Cannot find module webpack-cli,@vue-devtools/build-tools等
  5. 算法:回溯十三 Subsets II不重复的数组子集(3种解法)
  6. ae合成复制脚本_【脚本】AE脚本精选系列 | 合成复制脚本 True Comp Duplicator v3.9.7...
  7. 中国科技统计年鉴Excel版本(1991-2021年)
  8. 音王点歌系统怎么连接服务器,KTV音响设备怎么连接?点歌机怎么连接?学习下...
  9. IOS微信内置浏览器音频加载问题
  10. 狼奔权限管理系统开源啦
  11. BERT |(3)BERT模型的使用--pytorch的代码解释
  12. Matlab显示图像三维信息
  13. Qt QWidget播放视频背景图片闪烁
  14. 圣天诺(LDK)加密锁 云授权,不用使用硬件的加密锁(加密狗)
  15. DevOps流水线(1)什么是Pipeline流水线?
  16. 联想拯救者pro装华为鸿蒙OS,网友Mate 40 Pro+升级鸿蒙2.0:谷歌服务照样能用
  17. 代驾APP_第一章_项目环境搭建_第一节
  18. gocolly-登录(5)
  19. stem在计算机课程中的应用,浅谈小学信息技术课程中STEAM教育的应用
  20. 论计算机网络的安全与监控

热门文章

  1. 互联网日报 | 2月8日 星期一 | 乐视回应App图标“欠122亿”;中国联通成立联通数科;高德地图上线13万个旅游厕所信息...
  2. JavaScript - 获取、修改 title 元素的内容
  3. 正则表达式re中的group含义
  4. 安信可 Telink_825x 环境搭建
  5. 钩子(HOOK)函数教程
  6. Unity序列化——Assets序列化
  7. 01 社会网络分析基础理论!
  8. 小米9开发版刷稳定版心得体验
  9. 交安ABC考试单选练习题库
  10. 双指缩放canvas图片_JS实现移动端双指缩放和旋转方法