> For the complete documentation index, see [llms.txt](https://wentao-shao.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://wentao-shao.gitbook.io/leetcode/binary-tree/285.inorder-successor-in-bst.md).

# 285.Inorder-Successor-in-BST

## 285. Inorder Successor in BST

## 题目地址

<https://leetcode.com/problems/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.

The successor of a node p is the node with the smallest key greater than p.val.

Example 1:
Input: root = [2,1,3], p = 1
Output: 2
Explanation: 1's in-order successor node is 2. Note that both p and the return value is of TreeNode type.

Example 2:
Input: root = [5,3,6,2,4,null,null,1], p = 6
Output: null
Explanation: There is no in-order successor of the current node, so the answer is null.

Note:
If the given node has no in-order successor in the tree, return null.
It's guaranteed that the values of the tree are unique.
```

## 代码

### Approach 1: Iterative Inorder Traversal

```java
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
        if (p.right != null) {
        p = p.right;
        while (p.left != null) {
          p = p.left;
        }
        return p;
      }

      // the successor is somewhere upper in the tree
      ArrayDeque<TreeNode> stack = new ArrayDeque<>();
      int inorder = Integer.MIN_VALUE;

      // inorder traversal : left -> node -> right
      while (!stack.isEmpty() || root != null) {
        // 1. go left till you can
        while (root != null) {
          stack.push(root);
          root = root.left;
        }
        // 2. all logic around the node
        root = stack.pop();
        // if the previous node was equal to p
          // then the current node is its successor
        if (inorder == p.val) return root;
        inorder = root.val;

        // 3. go one step right
        root = root.right;
      }

      return null;
    }
}
```


---

# 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://wentao-shao.gitbook.io/leetcode/binary-tree/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.
