230. Kth Smallest Element in a BST
来源:互联网 发布:java 实现syslog 编辑:程序博客网 时间:2024/05/21 11:12
题目描述【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.
找出二叉搜索树的第k小的数,用递归再排序
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */void f(TreeNode * root, vector<int>& re){ if(!root) return; re.push_back(root->val); f(root->left,re); f(root->right,re);}class Solution {public: int kthSmallest(TreeNode* root, int k) { if(!root) return 0; vector<int>re; f(root,re); sort(re.begin(),re.end()); return re[k-1]; }};
阅读全文
0 0
- 230.Kth Smallest Element in a BST
- 230. Kth Smallest Element in a BST
- 230. Kth Smallest Element in a BST
- 230. Kth Smallest Element in a BST
- 230. Kth Smallest Element in a BST
- 230. Kth Smallest Element in a BST
- 230. Kth Smallest Element in a BST
- 230. Kth Smallest Element in a BST
- 230. Kth Smallest Element in a BST
- 230. Kth Smallest Element in a BST
- 230. Kth Smallest Element in a BST
- 230. Kth Smallest Element in a BST
- 230. Kth Smallest Element in a BST
- 230. Kth Smallest Element in a BST
- 230. Kth Smallest Element in a BST
- 230. Kth Smallest Element in a BST
- 230. Kth Smallest Element in a BST
- 230. Kth Smallest Element in a BST
- 【理论实践】c++11杂七杂八技术点
- 为什么Python中没有Switch/Case语句?
- 替换空格
- java 7 中 接口 的用法
- Java上传图片到OSS
- 230. Kth Smallest Element in a BST
- 详解SSD之垃圾回收GC
- 关于kettle初始化的那点事
- 17.5.28C组总结
- Springboot状态码总结
- spring事件机制_http://enjiex.iteye.com/blog/1070094
- iptables简单应用
- 【剑指offer】面试题 24:反转链表
- 类实现输出最高成绩