Skip to content

Completed Trees-1#1724

Open
subbu4061 wants to merge 2 commits intosuper30admin:masterfrom
subbu4061:master
Open

Completed Trees-1#1724
subbu4061 wants to merge 2 commits intosuper30admin:masterfrom
subbu4061:master

Conversation

@subbu4061
Copy link
Copy Markdown

No description provided.

@super30admin
Copy link
Copy Markdown
Owner

Strengths:

  • You provided two different approaches to solve the problem, showing a good understanding of BST properties.
  • Solution 1 is well-implemented with proper early termination and efficient inorder traversal.
  • The code is clean and well-commented.

Areas for Improvement:

  • In Solution 2, the recursion should stop once the flag is set to false to avoid unnecessary traversals. You can modify the helper function to return early if the flag is false. For example:
    private void helper(TreeNode root, long min, long max) {
        if (root == null || !flag) return;  // early termination if flag is false
        if (root.val <= min || root.val >= max) {
            flag = false;
            return;
        }
        helper(root.left, min, root.val);
        helper(root.right, root.val, max);
    }
    
  • Alternatively, you can make the helper function return a boolean to allow early termination without a class variable.
  • Note that for Solution 2, you used long for min and max to handle integer boundaries. This is correct, but be aware of the constraints.
  • The problem only asks for one solution, but it's great that you provided two. However, ensure that all solutions are optimal.

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