Inorder Successor in BST
Problem Descriptionβ
Visit LeetCode for the full problem description.
Solutionsβ
Solution 1: C# (Best: 196 ms)β
| Metric | Value |
|---|---|
| Runtime | 196 ms |
| Memory | N/A |
| Date | 2018-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β
| Approach | Time | Space |
|---|---|---|
| Solution | To be analyzed | To be analyzed |