Skip to content

Instantly share code, notes, and snippets.

@RakhmedovRS
Created January 15, 2023 21:15
Show Gist options
  • Select an option

  • Save RakhmedovRS/cf16254b30ce548661334742968d9193 to your computer and use it in GitHub Desktop.

Select an option

Save RakhmedovRS/cf16254b30ce548661334742968d9193 to your computer and use it in GitHub Desktop.
public boolean isValidBST(TreeNode root)
{
Integer[] prev = new Integer[1];
return isValidBST(root, prev);
}
public boolean isValidBST(TreeNode root, Integer[] prev)
{
boolean left = true;
if (root.left != null)
{
left = isValidBST(root.left, prev);
}
if (prev[0] != null && prev[0] >= root.val)
{
return false;
}
prev[0] = root.val;
boolean right = true;
if (root.right != null)
{
right = isValidBST(root.right, prev);
}
return left && right;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment