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 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 = 13/ \1 4\2
Output: 1
Example 2:
Input: root = [5,3,6,2,4,null,null,1], k = 35/ \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?
这道题目是经典的查找二叉树中第K小的元素,标准做法就是中序遍历,找到第K个值。
递归的Python解法如下:
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = Noneclass Solution:def kthSmallest(self, root: TreeNode, k: int) -> int:def inorder(r):return inorder(r.left) + [r.val] + inorder(r.right) if r else []return inorder(root)[k - 1]
迭代的C++解法如下:
/*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/
class Solution {public:int kthSmallest(TreeNode *root, int k) {stack<TreeNode *> s;TreeNode *current = root;TreeNode *prev = nullptr;while (current || !s.empty()) {while (current) {s.push(current);current = current->left;}current = s.top();s.pop();k--;if (k == 0) {break;}prev = current;current = current->right;}return current->val;}
};
LeetCode 230. Kth Smallest Element in a BST--C++,Python解法--面试真题--找二叉树中第K小的元素相关推荐
- [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 (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
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 来查 ...
- 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. ...
- 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 204. Count Primes--从一开始的质数个数--Python解法--面试算法题
题目地址:Count Primes - LeetCode Count the number of prime numbers less than a non-negative number, n. E ...
最新文章
- 主键id 请求参数用什么类型_中小型项目用SpringBoot太大了,不如尝试下Martian
- 关于水晶报表的一些错误
- 基于VUE的前端crypto-js aes加密与解密
- linux6.5dns装什么,CentOS6.5安装DNS服务
- 初一模拟赛总结(4.7)
- Let's Encrypt(开源SSL证书管理工具)
- 法的详细步骤_空气能热水工程安装步骤讲解
- 基于matlab的数字水印技术研究,MATLAB在数字水印技术研究中的应用
- ef core code first from exist db
- ios怎么引入masonry_iOS Masonry的使用需要注意的地方
- Django Rest framework实现流程
- WMware15和虚拟机Win10镜像文件网盘分享
- SpringBoot搭建个人博客v1.0 - 博客管理功能实现(七)
- 推荐一个比较好的游戏源码下载网站
- python二级题库(百分之九十原题) 刷题软件推荐 第六套
- A Game of Thrones(41)
- 服务器运行状态监控工具
- 计算机ip无法连接打印机,网络打印机无法连接的原因与解决办法-电脑故障
- 计算机的存储器体系结构,计算机体系结构设计 第05章 存储器体系结构设计.pptx...
- Android碎片化与兼容性问题的元凶
热门文章
- Redis数据库简介与(CentOS 7)编译安装
- python的类属性和方法_Python中类属性、实例属性和实例方法的区别
- 腾讯云“抢救”微盟!开 766 次在线会议、调拨 100 多台服务器、闹钟只敢定 2 小时...
- 美国微生物科学院22年院士公布!舒跃龙、黄力、卢洪洲、赵国屏!
- NanoPlot:三代纳米孔测序数据质量评估
- 植物根际微生物组也有昼夜节律
- Error in x$e : $ operator is invalid for atomic vectors
- python使用matplotlib可视化、自定义设置Y轴刻度标签字体的大小( setting axis ticks size in matplotlib y axis)
- R语言ggplot2可视化使用geom_ribbon()函数向ggplot2图添加置信度带(Confidence Band、Confidence Interval)
- pandas读写结构化数据(read_csv,read_table, read_excel, read_html, read_sql)