LeetCode OJ 之 Kth Smallest Element in a BST(BST中的第k小元素)
来源:互联网 发布:免费java教程视频下载 编辑:程序博客网 时间:2024/06/07 01:01
题目:
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?
思路:
1、中序遍历中查找
2、遍历每个结点的左、右子树中的结点个数
代码1:
/** * 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) { int result = 0; helper(root ,k , result); return result; } void helper(TreeNode * root , int &k , int &result) { if(root == NULL) return; helper(root->left ,k , result); k--;//每遍历一次,k-1,由于中序遍历是按从小到大遍历,因此遍历到第k次时,得到的数据就是第k小 if(k == 0) { result = root->val; return; } helper(root->right ,k , result); } };
代码2(非递归中序遍历):
/** * 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) { if(root == NULL) return -1; TreeNode *p = root; stack<TreeNode *> s; while(p || !s.empty()) { if(p) { s.push(p); p = p->left; } else { p = s.top(); s.pop(); k--; if(k == 0) return p->val; p = p->right; } } return -1; }};
代码3:
/** * 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) { int leftCount = helper(root->left); if(k == leftCount + 1) //如果左子树中有k-1个结点,说明当前结点是第k小结点 return root->val; if(k < leftCount + 1) //如果左子树超过k个结点,则对左子树调用,注意此时k不用改变 return kthSmallest(root->left , k); else return kthSmallest(root->right , k - leftCount - 1);//调用右子树,此时k要变化 } int helper(TreeNode * root) { if(root == NULL) return 0; return 1 + helper(root->left) + helper(root->right); } };
0 0
- LeetCode OJ 之 Kth Smallest Element in a BST(BST中的第k小元素)
- Kth Smallest Element in a BST 找二叉搜索树中的第k小的元素
- LeetCode 230. Kth Smallest Element in a BST(二叉搜索树中的第K个元素)
- Kth Smallest Element in a BST 寻找二叉排序树中第k小元素
- leetcode_230. Kth Smallest Element in a BST 求二叉搜索树中的第k小的元素,中序遍历法
- LeetCode-230. Kth Smallest Element in a BST (JAVA)二叉树第k小的数字
- leetcode230---Kth Smallest Element in a BST(BST中寻找第K小)
- 【二叉树】BST第K小值【230. Kth Smallest Element in a BST】
- <LeetCode OJ> 230. Kth Smallest Element in a BST
- leetcode oj java 230. 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
- Jquery autocomplete(自动补全)demo+cookie自动补全
- 数据结构-图
- 手动脱KBys Packer(0.28)壳实战
- osx 的adb问题
- 新交互英语外挂全自动版
- LeetCode OJ 之 Kth Smallest Element in a BST(BST中的第k小元素)
- Android 位图显示及位图模式的一些总结
- jsp页面出现Multiple annotations found at this line: - The superclass "javax.servlet.http.HttpServlet" w
- JAVA版暴力破解含验证码
- could not initialize proxy - no Session
- Android消息处理机制Thread、Handler、Looper
- ERLANG recon使用示例
- 小白学习之路(三):java ArrayList源码add操作和remove操作学习
- OSG学习——配置问题