Skip to content

Create trres1.java - #1756

Open
Praniksha123 wants to merge 1 commit into
super30admin:masterfrom
Praniksha123:master
Open

Create trres1.java#1756
Praniksha123 wants to merge 1 commit into
super30admin:masterfrom
Praniksha123:master

Conversation

@Praniksha123

Copy link
Copy Markdown

No description provided.

@super30admin

Copy link
Copy Markdown
Owner

Validate Binary Search Tree (trres1.java)

Your solution for the Validate Binary Search Tree problem has several critical issues:

  1. Null handling: if(root==null) return false; is incorrect. An empty tree should be considered a valid BST. The correct base case should return true for null.

  2. Ignored recursive results: You're calling isValidBST(root.left) and isValidBST(root.right) but completely ignoring their return values. You need to check if both subtrees are valid BSTs.

  3. Incorrect comparison: root.val > root.left compares an integer with a TreeNode reference, which doesn't make sense. You should compare root.val with root.left.val (and handle null cases).

  4. Missing BST property: The BST property requires that ALL nodes in the left subtree are less than the root, and ALL nodes in the right subtree are greater than the root. Your solution only checks immediate children, which is insufficient.

  5. Recommended approach: Consider using an in-order traversal approach where you track the previous node and ensure each node's value is greater than the previous one. Alternatively, use a helper function that passes valid range (min, max) bounds for each node.

Here's a corrected version using the range approach:

public boolean isValidBST(TreeNode root) {
    return helper(root, Long.MIN_VALUE, Long.MAX_VALUE);
}

private boolean helper(TreeNode node, long min, long max) {
    if (node == null) return true;
    if (node.val <= min || node.val >= max) return false;
    return helper(node.left, min, node.val) && helper(node.right, node.val, max);
}

VERDICT: NEEDS_IMPROVEMENT


Construct Binary Tree from Preorder and Inorder Traversal

Strengths:

  • Your buildTree solution correctly implements the optimal algorithm using a HashMap for O(1) lookups
  • The recursive structure is clean and mirrors the reference solution
  • Time and space complexity match the optimal solution

Areas for improvement:

  • Remove unrelated code: The isValidBST class is for a completely different problem (Validate Binary Search Tree). Including it in your submission creates confusion and suggests you may have mixed up files. Each problem should have its own clean solution file.
  • Bug in isValidBST: While not part of this evaluation, note that your isValidBST solution has serious issues - it doesn't properly validate BST properties (you can't just compare a node with its immediate children; you need to track valid ranges or use in-order traversal).
  • Consider adding comments: Brief comments explaining the algorithm would improve readability.

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