C# InOrder Recursive Traversal Solution (Accepted)


  • 1
    S
    public class Solution 
    {
            TreeNode prevNode = null;
            
            public bool IsValidBST(TreeNode currentNode)
            {
                return IsBSTInInOrderRecursion(currentNode);
            }
    
            private bool IsBSTInInOrderRecursion(TreeNode currentNode)
            {
                if (currentNode == null)
                    return true;
    
                bool result = IsBSTInInOrderRecursion(currentNode.left);
    
                if (result == false)
                    return false;
    
                if (prevNode != null && currentNode.val <= prevNode.val)
                    return false;
    
                prevNode = currentNode;
    
                return IsBSTInInOrderRecursion(currentNode.right);
            }
    }
    

Log in to reply
 

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