> For the complete documentation index, see [llms.txt](https://anand-aryan.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://anand-aryan.gitbook.io/leetcode/285.-inorder-successor-in-bst.md).

# 285. Inorder Successor in BST

Given a binary search tree and a node in it, find the in-order successor of that node in the BST.

Note: If the given node has no in-order successor in the tree, return null.

## Solution <a href="#solution" id="solution"></a>

```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 TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
        if (p == null) return null;

        if (p.right != null) {

            TreeNode runner = p.right;
            while (runner.left != null) {
                runner = runner.left;
            }

            return runner;

        } else {

            Map<TreeNode, TreeNode> map = new HashMap<>();
            boolean exist = search(root, p, map);
            if (!exist) {
                return null;
            } else {

                TreeNode parent = map.get(p);
                TreeNode runner = p;
                while (parent != null && parent.right != null && runner != null && parent.right.equals(runner)) {
                    runner = parent;
                    parent = map.get(runner);
                }

                return parent;
            }
        }

    }

    private boolean search(TreeNode root, TreeNode p, Map<TreeNode, TreeNode> map) {
        if (root == null) {
            return false;
        }

        if (root.equals(p)) {
            return true;
        }

        boolean leftFlag = false;
        if (root.left != null) {
            map.put(root.left, root);
            leftFlag = search(root.left, p, map);
        }

        boolean rightFlag = false;
        if (root.right != null) {
            map.put(root.right, root);
            rightFlag = search(root.right, p, map);
        }

        return leftFlag || rightFlag;
    }

}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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://anand-aryan.gitbook.io/leetcode/285.-inorder-successor-in-bst.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.
