C# solution accepted


  • 0
    Y

    /**

    • 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 bool IsValidBST(TreeNode root) {
      if(root == null || (root.left == null && root.right==null))
      {
      return true;
      }

       return IsValidBST(root, long.MaxValue, long.MinValue);
      

      }

      public bool IsValidBST(TreeNode root, long maxValue, long minValue)
      {
      if(root == null)
      {
      return true;
      }

       if(root.val >= maxValue || root.val <= minValue)
       {
           return false;
       }
       
       return IsValidBST(root.left, (long)root.val, minValue) && IsValidBST(root.right, maxValue,    (long)root.val);
      

      }
      }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.