Skip to content

Complete Trees-1 assignment - #1757

Open
tejbharath wants to merge 2 commits into
super30admin:masterfrom
tejbharath:master
Open

Complete Trees-1 assignment#1757
tejbharath wants to merge 2 commits into
super30admin:masterfrom
tejbharath:master

Conversation

@tejbharath

Copy link
Copy Markdown

No description provided.

@super30admin

Copy link
Copy Markdown
Owner

Validate Binary Search Tree (BinaryTreePreOrderInOrderTraversal.java)

The student has submitted a solution for a completely different problem (Binary Tree Construction from Preorder and Inorder Traversal) instead of the Validate Binary Search Tree problem. While the code itself appears to be a reasonable solution for the problem it was intended for, it does not address the actual problem at hand.

To correctly solve the Validate Binary Search Tree problem, you should:

  1. Use an in-order traversal approach (as shown in the reference solution) or a recursive approach with min/max bounds.
  2. In an in-order traversal of a valid BST, the values should be in strictly increasing order.
  3. Keep track of the previous node visited during the traversal and ensure each node's value is greater than the previous one.

Here's a sketch of the in-order traversal approach:

class Solution {
    TreeNode prev;
    
    public boolean isValidBST(TreeNode root) {
        if (root == null) return true;
        
        // Check left subtree
        if (!isValidBST(root.left)) return false;
        
        // Check current node
        if (prev != null && prev.val >= root.val) return false;
        prev = root;
        
        // Check right subtree
        return isValidBST(root.right);
    }
}

VERDICT: NEEDS_IMPROVEMENT


Construct Binary Tree from Preorder and Inorder Traversal (ValidBST.java)

It appears you have submitted a solution for a different problem (Validate Binary Search Tree) instead of the required problem (Construct Binary Tree from Preorder and Inorder Traversal).

To solve the correct problem, you need to:

  1. Use the first element of preorder as the root value.
  2. Find this root value's position in inorder - everything to the left is the left subtree, everything to the right is the right subtree.
  3. Recursively build the left subtree using the corresponding portions of both arrays.
  4. Recursively build the right subtree similarly.
  5. Use a HashMap to store inorder indices for O(1) lookup, achieving O(n) time complexity.

Please re-submit with the correct solution for the given problem.

VERDICT: NEEDS_IMPROVEMENT

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