|
| 1 | +<p>Given the <code>root</code> of a binary search tree and a node <code>p</code> in it, return <em>the in-order successor of that node in the BST</em>. If the given node has no in-order successor in the tree, return <code>null</code>.</p> |
| 2 | + |
| 3 | +<p>The successor of a node <code>p</code> is the node with the smallest key greater than <code>p.val</code>.</p> |
| 4 | + |
| 5 | +<p> </p> |
| 6 | +<p><strong class="example">Example 1:</strong></p> |
| 7 | +<img alt="" src="https://assets.leetcode.com/uploads/2019/01/23/285_example_1.PNG" style="width: 122px; height: 117px;" /> |
| 8 | +<pre> |
| 9 | +<strong>Input:</strong> root = [2,1,3], p = 1 |
| 10 | +<strong>Output:</strong> 2 |
| 11 | +<strong>Explanation:</strong> 1's in-order successor node is 2. Note that both p and the return value is of TreeNode type. |
| 12 | +</pre> |
| 13 | + |
| 14 | +<p><strong class="example">Example 2:</strong></p> |
| 15 | +<img alt="" src="https://assets.leetcode.com/uploads/2019/01/23/285_example_2.PNG" style="width: 246px; height: 229px;" /> |
| 16 | +<pre> |
| 17 | +<strong>Input:</strong> root = [5,3,6,2,4,null,null,1], p = 6 |
| 18 | +<strong>Output:</strong> null |
| 19 | +<strong>Explanation:</strong> There is no in-order successor of the current node, so the answer is <code>null</code>. |
| 20 | +</pre> |
| 21 | + |
| 22 | +<p> </p> |
| 23 | +<p><strong>Constraints:</strong></p> |
| 24 | + |
| 25 | +<ul> |
| 26 | + <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> |
| 27 | + <li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li> |
| 28 | + <li>All Nodes will have unique values.</li> |
| 29 | +</ul> |
0 commit comments