Leetcode 272. Closest Binary Search Tree Value II (Hard) (cpp)
来源:互联网 发布:lg扫地机器人.知乎 编辑:程序博客网 时间:2024/05/16 15:59
Leetcode 272. Closest Binary Search Tree Value II (Hard) (cpp)
Tag: Tree, Stack
Difficulty: Hard
/*272. Closest Binary Search Tree Value II (Hard)Given a non-empty binary search tree and a target value, find k values in the BST that are closest to the target.Note:Given target value is a floating point.You may assume k is always valid, that is: k ≤ total nodes.You are guaranteed to have only one unique set of k values in the BST that are closest to the target.Follow up:Assume that the BST is balanced, could you solve it in less than O(n) runtime (where n = total nodes)?Hint:Consider implement these two helper functions:getPredecessor(N), which returns the next smaller node to N.getSuccessor(N), which returns the next larger node to N.Try to assume that each node has a parent pointer, it makes the problem much easier.Without parent pointer we just need to keep track of the path from the root to the current node using a stack.You would need two stacks to track the path in finding predecessor and successor node separately.*//*** 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:vector<int> closestKValues(TreeNode* root, double target, int k) {stack<TreeNode*> suc, pre;vector<int> res;iniPreSta(root, target, pre);iniSucSta(root, target, suc);if (!suc.empty() && !pre.empty() && suc.top()->val == pre.top()->val) {getNextPredecessor(pre);}while (k-- > 0) {if (suc.empty()) {res.push_back(getNextPredecessor(pre));}else if (pre.empty()) {res.push_back(getNextSuccessor(suc));}else {if (abs((double)pre.top()->val - target) < abs((double)suc.top()->val - target)) {res.push_back(getNextPredecessor(pre));}else {res.push_back(getNextSuccessor(suc));}}}return res;}private:void iniPreSta(TreeNode* root, double target, stack<TreeNode*>& pre) {while (root != NULL) {if (root->val < target) {pre.push(root);root = root->right;}else if (root->val > target) {root = root->left;}else {pre.push(root);break;}}}void iniSucSta(TreeNode* root, double target, stack<TreeNode*>& suc) {while (root != NULL) {if (root->val > target) {suc.push(root);root = root->left;}else if (root->val < target) {root = root->right;}else {suc.push(root);break;}}}int getNextSuccessor(stack<TreeNode*>& suc) {TreeNode* cur = suc.top();suc.pop();int res = cur->val;cur = cur->right;while (cur != NULL) {suc.push(cur);cur = cur->left;}return res;}int getNextPredecessor(stack<TreeNode*>& pre) {TreeNode* cur = pre.top();pre.pop();int res = cur->val;cur = cur->left;while (cur != NULL) {pre.push(cur);cur = cur->right;}return res;}};
0 0
- Leetcode 272. Closest Binary Search Tree Value II (Hard) (cpp)
- [Leetcode]Closest Binary Search Tree Value II
- [leetcode] 272. Closest Binary Search Tree Value II 解题报告
- LeetCode 272. Closest Binary Search Tree Value II
- Leetcode 272. Closest Binary Search Tree Value II
- [Leetcode] 272. Closest Binary Search Tree Value II 解题报告
- 272. Closest Binary Search Tree Value II
- Leetcode 270. Closest Binary Search Tree Value (cpp)
- leetcode 272: Closest Binary Search Tree Value II
- LeetCode 题解(250) : Closest Binary Search Tree Value II
- [LeetCode282]Closest Binary Search Tree Value II
- Closest Binary Search Tree Value II
- Closest Binary Search Tree Value II
- LeetCode 272. Closest Binary Search Tree Value II(二叉搜索树查找)
- [Leetcode]Closest Binary Search Tree Value
- leetcode 270: Closest Binary Search Tree Value
- *LeetCode-Closest Binary Search Tree Value
- [leetcode 270] Closest Binary Search Tree Value
- [leetcode][155] Min Stack
- Spring架构图
- TEC1401.Report开发技术总结 - 第九章 使用BI Publisher开发报表-使用BI Publisher创建RTF模板的语法(4/5)
- HDUoj 2141 Can you find it?(排序+二分)
- Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal
- Leetcode 272. Closest Binary Search Tree Value II (Hard) (cpp)
- TEC1401.Report开发技术总结 - 第十章 使用BI Publisher开发报表-其他注意事项(5/5)
- Notifycation使用入门
- Subsets
- 【LeetCode】 475. Heaters
- Leetcode 285. Inorder Successor in BST (Medium) (cpp)
- 简单几步实现网络音乐播放器(Python爬虫版百度FM)
- 使用C++扩展Python的功能
- Leetcode 298. Binary Tree Longest Consecutive Sequence (Medium) (cpp)