Leetcode: Closest Binary Search Tree Value II
2015-12-24 13:14
537 查看
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.
用一个栈一个队列,用recursion写inorder Traversal, Time: O(N+K), Space: O(N)
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public List<Integer> closestKValues(TreeNode root, double target, int k) { List<Integer> res = new ArrayList<Integer>(); LinkedList<Integer> stack = new LinkedList<Integer>(); LinkedList<Integer> queue = new LinkedList<Integer>(); inorder(root, target, stack, queue); for (int i=0; i<k; i++) { if (stack.isEmpty()) res.add(queue.poll()); else if (queue.isEmpty()) res.add(stack.pop()); else { int s = stack.peek(); int q = queue.peek(); if (Math.abs(s-target) < Math.abs(q-target)) { res.add(s); stack.pop(); } else { res.add(q); queue.poll(); } } } return res; } public void inorder(TreeNode root, double target, LinkedList<Integer> stack, LinkedList<Integer> queue) { if (root == null) return; inorder(root.left, target, stack, queue); if (root.val < target) { stack.push(root.val); } else { queue.offer(root.val); } inorder(root.right, target, stack, queue); } }
Better Idea: 只用一个大小为K的队列的方法:Time O(N), Space O(K)
前K个数直接加入队列,之后每来一个新的数(较大的数),如果该数和目标的差,相比于队头的数离目标的差来说,更小,则将队头拿出来,将新数加入队列。如果该数的差更大,则直接退出并返回这个队列,因为后面的数更大,差值也只会更大。
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public List<Integer> closestKValues(TreeNode root, double target, int k) { //List<Integer> res = new ArrayList<Integer>(); LinkedList<TreeNode> stack = new LinkedList<TreeNode>(); LinkedList<Integer> queue = new LinkedList<Integer>(); while (!stack.isEmpty() || root!=null) { if (root != null) { stack.push(root); root = root.left; } else { root = stack.pop(); if (queue.size() < k) { queue.offer(root.val); } else { if (Math.abs(root.val-target) < Math.abs(queue.peek()-target)) { queue.poll(); queue.offer(root.val); } else break; } root = root.right; } } return (List<Integer>)queue; } }
相关文章推荐
- UI学习
- UEditor1.3.6图片上传
- [ActionScript 3.0] AS3 GUID(全局唯一标识符)
- JDBC连接池的testQuery/validationQuery设置
- iOS小技巧15-改变UITabBarItem 字体颜色
- 使用buildroot制作交叉编译工具
- js下读取input中的value值
- IOS中UITableViewCell使用详解
- mybatis 中insert语句报如下错误: Incorrect integer value: '' for column 'pic_len' at row 1
- UILabel上的文字 单独改变 属性
- 分割视图控制器(UISplitViewController)
- PHP 性能分析第二篇: Xhgui In-Depth
- UITableViewController和UIRefreshControl实现下拉刷新功能
- ueditor使用会于第三方发生冲突
- UIScrollView中contentSize、contentInset、contentOffset介绍
- 解决The current branch is not configured for pull No value for key branch.master.merge found in confi
- CoreBluetooth框架(转)
- UILabel
- String,StringBuffer与StringBuilder的区别
- iOS 教你学UI控件的封装