Skip to main content

Inorder Successor in BST

LeetCode Link

Problem Description​

Visit LeetCode for the full problem description.


Solutions​

Solution 1: C# (Best: 196 ms)​

MetricValue
Runtime196 ms
MemoryN/A
Date2018-09-25
Solution
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode InorderSuccessor(TreeNode root, TreeNode p) {
TreeNode current = root;
TreeNode succ = null;
while (current != null)
{

if (p.val < current.val)
{
succ = current;
current = current.left;

}
else
{
current = current.right;
}
}
return succ;
}
}

Complexity Analysis​

ApproachTimeSpace
SolutionTo be analyzedTo be analyzed