# Get Keys In Binary Search Tree In Given Range

Get the list of keys in a given binary search tree in a given range\[min, max] in ascending order, both min and max are inclusive.

**Examples**

&#x20;       5

&#x20;     /    \\

&#x20;   3        8

&#x20; /   \        \\

&#x20;1     4        11

get the keys in \[2, 5] in ascending order, result is  \[3, 4, 5]

**Corner Cases**

* What if there are no keys in the given range? Return an empty list in this case.

**How is the binary tree represented?**

We use the level order traversal sequence with a special symbol "#" denoting the null node.

**For Example:**

The sequence \[1, 2, 3, #, #, 4] represents the following binary tree:

&#x20;   1

&#x20; /   \\

&#x20;2     3

&#x20;     /

&#x20;   4

```
public class Solution {
  List<Integer> result = new ArrayList();

  public List<Integer> getRange(TreeNode root, int min, int max) {
    if (root == null) return new ArrayList();
    //base case 

    //recusive rule
    //check left child valid?
    //check self valid?
    //check right child valid?
    //return result
    getRange(root.left, min, max);

    int key = root.key;
    if (min <= key && key <= max){
      result.add(key);
    }

    getRange(root.right, min , max);
    
    return result;
  }
}

```


---

# 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://llssff.gitbook.io/coding-problems/tree-traversal/get-keys-in-binary-search-tree-in-given-range.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.
