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
/**
* 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;
}
}
Last updated