leetCode 230.Kth Smallest Element in a BST
来源:互联网 发布:35岁考本科 知乎 编辑:程序博客网 时间:2024/06/05 23:39
//找到一个二叉查找树的第K小的个元素
//中序遍历二叉查找树,第K次访问的值就是整个二叉树的第K小的元素
struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} };int kthSmallest(TreeNode* root, int k) { stack<TreeNode*> ms; TreeNode *p = root,*q; while(!ms.empty()||p){ if(p){ ms.push(p); p=p->left; } else{ q = ms.top(); ms.pop(); k--; if(k==0) return q->val; p=q->right; } }}//关键是掌握非递归的中序遍历的方法。
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
- 类
- [World Final2015试题选做]
- SQL 注入
- JQuery选择器整理
- 灰度图像--图像增强 双边滤波 Bilateral Filtering
- leetCode 230.Kth Smallest Element in a BST
- 如何用ddms看占用内存的方法.
- php调用webservice的几种方法
- Effective STL 条款9
- openstack keystone v2 to v3
- 格式化金额,每隔三位加逗号
- 浅谈PHP进程管理
- 《iOS Human Interface Guidelines》——App Icon
- Android中ExpandableListView中嵌套ListView