原题网址:https://leetcode.com/problems...
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.
题意:在二叉搜索树当中找到离target最近的K个数。
解题思路:
由于二叉搜索数的inorder中序遍历是有序的,比如例子中的树,中序遍历为[1,2,3,4,5]。我们可以利用这一特性,初始化一个双端队列Deque,用来存放k个数,然后用递归的方式,先走到the most left(也就是例子中的1),不断的向Deque中加入元素,直到元素装满,也就是Deque的size()到k个了,将当前元素与target的距离和队列头部与target的距离进行对比,如果当前元素的距离更小,则用Deque的pollFirst()方法将头部吐出,把当前元素从addLast()加入。
Example:
Input: root = [4,2,5,1,3], target = 3.714286, and k = 2
4
/ \
2 5
/ \
1 3
Output: [4,3]
代码如下:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
/**
*直接在中序遍历的过程中完成比较,当遍历到一个节点时,
如果此时结果数组不到k个,我们直接将此节点值加入res中,
如果该节点值和目标值的差值的绝对值小于res的首元素和目标值差值的绝对值,
说明当前值更靠近目标值,则将首元素删除,末尾加上当前节点值,
反之的话说明当前值比res中所有的值都更偏离目标值,
由于中序遍历的特性,之后的值会更加的遍历,所以此时直接返回最终结果即可,
*/
public List closestKValues(TreeNode root, double target, int k) {
Deque deque = new ArrayDeque<>();
inorder(root, target, k, deque);
List res = new ArrayList<>(deque);
return res;
}
private void inorder(TreeNode root,
double target,
int k,
Deque deque) {
if (root == null) return;
inorder(root.left, target, k, deque);
if (deque.size() < k) {
deque.offer(root.val);
} else if (Math.abs(root.val - target) < Math.abs(deque.peekFirst()-target) ) {
deque.pollFirst();
deque.addLast(root.val);
}
inorder(root.right, target, k, deque);
}
}
还有一种用Stack完成的方式,思路和递归相同,但是iterative的写法,也有必要掌握,必须把控Stack是否为空的情况,当前的node为null,但是stack中仍然有元素,依然需要进行比较。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List closestKValues(TreeNode root, double target, int k) {
Deque deque = new ArrayDeque<>();
Stack stack = new Stack<>();
TreeNode cur = root;
while(cur != null || !stack.isEmpty()) {
while(cur != null) {
stack.push(cur);
cur = cur.left;
}
cur = stack.pop();
if (deque.size() < k) {
deque.addLast(cur.val);
} else if (Math.abs(cur.val - target) < Math.abs(deque.peekFirst() - target)) {
deque.pollFirst();
deque.addLast(cur.val);
}
cur = cur.right;
}
List res = new ArrayList<>(deque);
return res;
}
}