Skip to content

solution Trees 1#1738

Open
shakthinandana wants to merge 5 commits into
super30admin:masterfrom
shakthinandana:master
Open

solution Trees 1#1738
shakthinandana wants to merge 5 commits into
super30admin:masterfrom
shakthinandana:master

Conversation

@shakthinandana
Copy link
Copy Markdown

No description provided.

@super30admin
Copy link
Copy Markdown
Owner

Validate BST (validate_bst.py)

Your solution is correct and efficient in terms of time complexity. However, there are a few points to consider:

  1. Space Complexity: You mentioned O(n) in the comment, but the actual space complexity is O(h) where h is the height of the tree. In the worst case (a skewed tree), h = n, so it becomes O(n), but for a balanced tree it is O(log n). It's important to be precise.

  2. Early Termination: Currently, your code continues to traverse the entire tree even after finding an invalid node. This can be optimized by checking the flag and returning early from the helper function. However, since the helper function is recursive and does not return a value, it's tricky. You can modify the helper to return a boolean and use that to stop recursion. For example:

    def helper(root):
         if root is None:
             return True
         if not helper(root.left):
             return False
         if self.prev is not None and root.val <= self.prev.val:
             return False
         self.prev = root
         return helper(root.right)
    

    Then in isValidBST, you can just return helper(root). This way, the recursion stops as soon as an invalid condition is found.

  3. Use of Instance Variables: Using instance variables (self.flag and self.prev) is acceptable in Leetcode because each test case creates a new instance. However, in general, it is better to avoid mutable state when possible. You could use a nonlocal variable in Python (if using a nested function) or pass the state as parameters. But since you are using a class method, the current approach is fine.

  4. Edge Cases: Your solution handles edge cases (like an empty tree) correctly because the helper returns immediately for a null node.

Overall, your solution is good, but with a small optimization for early termination, it could be more efficient in cases where the invalidity is found early.

VERDICT: PASS


Construct Binary Tree from Preorder and Inorder Traversal (build_with_preorder_inorder.py)

Your solution is correct and efficient. You have successfully implemented the recursive approach with a hashmap to optimize the search for the root index in the inorder array. Here are a few suggestions for improvement:

  1. Variable Names: Consider using more descriptive variable names. For example, in_st and in_end could be renamed to left and right or start and end to clearly indicate the current segment of the inorder array being processed.

  2. Instance Variable: Using self.rootindex as an instance variable works for Leetcode, but it's important to note that if the same Solution instance were reused for multiple calls, rootindex would not be reset. To make the code more robust, you could pass the index as a parameter or use a local variable within buildTree and update it in the helper function (e.g., by returning the updated index). However, for the purpose of this problem, your approach is acceptable.

  3. Code Clarity: The code is generally clear, but adding comments to explain the purpose of the helper function and the parameters would be helpful for others reading the code.

  4. Edge Cases: Your solution handles the base case correctly (when in_st > in_end), which is good.

Overall, this is a solid implementation. Keep up the good work!

VERDICT: PASS

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