DEV Community 👩‍💻👨‍💻

Miss Pooja Anilkumar Patel
Miss Pooja Anilkumar Patel

Posted on • Updated on

98. Leetcode solution in CPP

class Solution {
  public boolean isValidBST(TreeNode root) {
    return isValidBST(root, null, null);
  }

  private boolean isValidBST(TreeNode root, TreeNode minNode, TreeNode maxNode) {
    if (root == null)
      return true;
    if (minNode != null && root.val <= minNode.val)
      return false;
    if (maxNode != null && root.val >= maxNode.val)
      return false;

    return isValidBST(root.left, minNode, root) &&
           isValidBST(root.right, root, maxNode);
  }
}

Enter fullscreen mode Exit fullscreen mode

leetcode

challenge

here is the link for the problem:
https://leetcode.com/problems/validate-binary-search-tree/submissions/

Top comments (0)

🌚 Browsing with dark mode makes you a better developer.

It's a scientific fact.