原題網址:https://leetcode.com/problems...node
Given a non-empty binary search tree and a target value, find k values in the BST that are closest to the target.數組
Note:less
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)?ide
Hint:code
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.leetcode
Without parent pointer we just need to keep track of the path from the root to the current node using a stack.get
You would need two stacks to track the path in finding predecessor and successor node separately.it
題意:在二叉搜索樹當中找到離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<Integer> closestKValues(TreeNode root, double target, int k) { Deque<Integer> deque = new ArrayDeque<>(); inorder(root, target, k, deque); List<Integer> res = new ArrayList<>(deque); return res; } private void inorder(TreeNode root, double target, int k, Deque<Integer> 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<Integer> closestKValues(TreeNode root, double target, int k) { Deque<Integer> deque = new ArrayDeque<>(); Stack<TreeNode> 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<Integer> res = new ArrayList<>(deque); return res; } }