LeetCode[230] Kth Smallest Element in a BST
来源:互联网 发布:多用户建站cms系统 编辑:程序博客网 时间:2024/05/22 09:54
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?
/** * 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 count;int kthSmallest(TreeNode* root, int k) {int result = 0;if (root->left != NULL && count < k)result = kthSmallest(root->left, k);count++;if (count == k)result = root->val;if (root->right != NULL && count < k)result = kthSmallest(root->right, k);return result;}Solution(): count(0) { };};
0 0
- #leetcode#Kth Smallest Element in a BST
- leetcode--Kth Smallest Element in a BST
- Leetcode|Kth Smallest Element in a BST
- [leetcode] Kth Smallest Element in a BST
- 【LeetCode】Kth Smallest Element in a BST
- LeetCode Kth Smallest Element in a BST
- 【leetcode】Kth Smallest Element in a BST
- LeetCode Kth Smallest Element in a BST
- leetcode: Kth Smallest Element in a BST
- Leetcode: Kth Smallest Element in a BST
- [LeetCode] Kth Smallest Element in a BST
- [LeetCode]Kth Smallest Element in a BST
- [Leetcode]Kth Smallest Element in a BST
- leetcode--Kth Smallest Element in a BST
- 【leetcode】Kth Smallest Element in a BST
- [leetcode] Kth Smallest Element in a BST
- [Leetcode]Kth Smallest Element in a BST
- Leetcode: Kth Smallest Element in a BST
- 5
- Java学习之路--多态
- javascript时间格式代码
- 前端开发 环境搭建
- Shell下的作业管理
- LeetCode[230] Kth Smallest Element in a BST
- Deepin搭建Qt5开发环境
- codeforces 719C Efim and Strange Grade (复杂模拟)
- 总结一下学习Designated Initializer的过程
- 简单的透明shader
- Ubuntu 14.04 设置静态IP地址全攻略
- Java 文件复制 基于字节流数据传输
- SpringMVC 的配置文件实例
- 面向对象基础部分