Skip to content

[IMP] Trees-1#1725

Open
amitmittal117 wants to merge 1 commit intosuper30admin:masterfrom
amitmittal117:master
Open

[IMP] Trees-1#1725
amitmittal117 wants to merge 1 commit intosuper30admin:masterfrom
amitmittal117:master

Conversation

@amitmittal117
Copy link
Copy Markdown

No description provided.

@super30admin
Copy link
Copy Markdown
Owner

Your solution for "Validate BST" is correct and efficient. Well done! Here are some points for improvement:

  1. Early Termination: Currently, even after finding an invalid node (setting flag to false), the recursion continues. You can add a check to stop the recursion early. For example, in the helper function, you can return early if the flag is already false. This would save time in some cases.

    Example modification:

    def helper(self, root):
         if root == None or not self.flag:
             return
    
  2. Avoid Instance Variables: While using instance variables is acceptable, it might be cleaner to avoid them by using a recursive function that returns values or by using a mutable object (like a list) to hold the state. However, this is a minor point and the current approach is common.

  3. Comment Clarity: Your time complexity comment says "O(2)" which is not standard. You probably meant O(n). Please use standard notation.

  4. Unnecessary Import: You have imported the copy module, but it is not used in the solution. You should remove unused imports.

  5. For the other solution (construct tree), note that the time complexity is actually O(n^2) due to the list slicing and the linear search. Consider using indices to avoid slicing for a more efficient solution.

Overall, your solution for the BST validation is good. With a small change for early termination, it would be even better.

@super30admin
Copy link
Copy Markdown
Owner

Strengths:

  • The solution correctly implements the in-order traversal to validate the BST.
  • The code is clean and well-commented.
  • The time and space complexity are optimal.

Areas for Improvement:

  1. Avoid using instance variables for state in recursive solutions. Instead, you can use a recursive function that returns a value or use a wrapper function that maintains state locally. For example, you can use a nested function and nonlocal variables (in Python) or pass state as parameters.
  2. Add short-circuiting: Once the flag is set to false, you should stop the traversal to save time. Currently, the traversal continues even after the tree is known to be invalid.
  3. The space complexity comment in the code says O(n), which is correct for the worst case, but it's better to specify that it is O(h) where h is the height of the tree.
  4. Remove unrelated code: The solution for "Construct Binary Tree from Preorder and Inorder Traversal" should not be included in the same file when submitting for "Validate BST".

Here is an improved version of your code that avoids instance variables and adds short-circuiting:

class Solution:
    def isValidBST(self, root: Optional[TreeNode]) -> bool:
        def in_order_traversal(node):
            nonlocal prev
            if not node:
                return True
            if not in_order_traversal(node.left):
                return False
            if prev is not None and prev.val >= node.val:
                return False
            prev = node
            return in_order_traversal(node.right)
        
        prev = None
        return in_order_traversal(root)

This version uses a nested function and nonlocal variable to maintain state, and it returns immediately when an invalid condition is found.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants