Validate BST using recursive in-order traversal


  • 0
    V
    /*
     * 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 {    
        TreeNode prev = null;
        public bool IsValidBST(TreeNode root) {
            if (root == null) {
                return true;
            }
    
            if (!IsValidBST(root.left)) { return false; }
            if (prev != null && prev.val >= root.val) { return false; }
            prev = root;
            return IsValidBST(root.right);
        }
    }
    

Log in to reply
 

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