230. Kth Smallest Element in a BSTs
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相关推荐
- [leetcode] 230. Kth Smallest Element in a BST 找出二叉搜索树中的第k小的元素
题目大意 https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/ 230. Kth Smallest Elem ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 230. Kth Smallest Element in a BST
题目: Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. ...
- 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 ...
- 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 ...
- [LeetCode] 230. Kth Smallest Element in a BST
题目内容 https://leetcode-cn.com/problems/kth-smallest-element-in-a-bst/ 给定一个二叉搜索树,编写一个函数 kthSmallest 来查 ...
- [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 ...
最新文章
- NYOJ 536 开心的mdd
- 《LED调光-DMX512灯光协义接收控制》转
- LinkedList常用方法笔记
- Vue:安装Vue Devtools调试工具简便方法解决Cannot find module webpack-cli,@vue-devtools/build-tools等
- 算法:回溯十三 Subsets II不重复的数组子集(3种解法)
- ae合成复制脚本_【脚本】AE脚本精选系列 | 合成复制脚本 True Comp Duplicator v3.9.7...
- 中国科技统计年鉴Excel版本(1991-2021年)
- 音王点歌系统怎么连接服务器,KTV音响设备怎么连接?点歌机怎么连接?学习下...
- IOS微信内置浏览器音频加载问题
- 狼奔权限管理系统开源啦
- BERT |(3)BERT模型的使用--pytorch的代码解释
- Matlab显示图像三维信息
- Qt QWidget播放视频背景图片闪烁
- 圣天诺(LDK)加密锁 云授权,不用使用硬件的加密锁(加密狗)
- DevOps流水线(1)什么是Pipeline流水线?
- 联想拯救者pro装华为鸿蒙OS,网友Mate 40 Pro+升级鸿蒙2.0:谷歌服务照样能用
- 代驾APP_第一章_项目环境搭建_第一节
- gocolly-登录(5)
- stem在计算机课程中的应用,浅谈小学信息技术课程中STEAM教育的应用
- 论计算机网络的安全与监控
热门文章
- 互联网日报 | 2月8日 星期一 | 乐视回应App图标“欠122亿”;中国联通成立联通数科;高德地图上线13万个旅游厕所信息...
- JavaScript - 获取、修改 title 元素的内容
- 正则表达式re中的group含义
- 安信可 Telink_825x 环境搭建
- 钩子(HOOK)函数教程
- Unity序列化——Assets序列化
- 01 社会网络分析基础理论!
- 小米9开发版刷稳定版心得体验
- 交安ABC考试单选练习题库
- 双指缩放canvas图片_JS实现移动端双指缩放和旋转方法