# Problem 272: Closest Binary Search Tree Value II

> <https://leetcode.com/problems/closest-binary-search-tree-value-ii/#/description>

![](/files/-Lpv9vcKEPOg1Exk7kon)

## 思路

* 用两个 stack 来维护，一个 small，存比 target 小的 node；另一个 large，存比 target 大的 nodes.
* 当取出一个 small value 的时候，再补充一个和 target 接近的 node，也就是 push 右子树进去。large value 同理。

```java
/**
 * 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> list = new ArrayList<>();
        Stack<TreeNode> small = new Stack<>();
        Stack<TreeNode> large = new Stack<>();
        TreeNode cur = root;
        while (cur != null) {
            if (cur.val >= target) {
                large.push(cur);
                cur = cur.left;
            } else {
                small.push(cur);
                cur = cur.right;
            }
        }

        while (k > 0) {
            if (small.isEmpty() && large.isEmpty()) {
                break;
            } else if (small.isEmpty()) {
                list.add(getLargeValue(large));
            } else if (large.isEmpty()) {
                list.add(getSmallValue(small));
            } else if (Math.abs(target - small.peek().val) < Math.abs(target - large.peek().val)) {
                list.add(getSmallValue(small));
            } else {
                list.add(getLargeValue(large));
            }

            k--;
        }


        return list;
     }

     private int getSmallValue(Stack<TreeNode> stack) {
         TreeNode node = stack.pop();
         TreeNode left = node.left;
         while (left != null) {
             stack.push(left);
             left = left.right;
         }

         return node.val;
     }

     private int getLargeValue(Stack<TreeNode> stack) {
         TreeNode node = stack.pop();
         TreeNode right = node.right;
         while (right != null) {
             stack.push(right);
             right = right.left;
         }

         return node.val;
     }

}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://liuyang89116.gitbook.io/my-leetcode-book/chapter_3_binary_tree_bfs-_dfs/problem-270-closest-binary-search-tree-value/problem-272-closest-binary-search-tree-value-ii.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
