96.Unique Binary Search Trees#260
Merged
SjxSubham merged 6 commits intoSjxSubham:mainfrom Oct 30, 2025
Merged
Conversation
Reviewer's GuideThis PR introduces two LeetCode problem solutions: a two-pass greedy algorithm for the Candy problem (135) and a top-down memoized DP for counting unique Binary Search Trees (96). Class diagram for Solution class in Unique Binary Search Trees (96)classDiagram
class Solution {
+int dp[20]
+int numTrees(int n)
}
Class diagram for Solution class in Candy problem (135)classDiagram
class Solution {
+int candy(vector<int>& ratings)
}
File-Level Changes
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey there - I've reviewed your changes - here's some feedback:
- The dp array in the Unique BST solution is declared as size 20 but needs to support indices up to n=20, so increase it to at least dp[21] (or use a vector) to avoid out-of-bounds access.
- Use a 64-bit type (long long or uint64_t) for Catalan numbers when n can be 20, since Catalan(20) exceeds 32-bit integer range and will overflow.
- This PR currently includes solutions for both 'Candy' and 'Unique Binary Search Trees'—consider splitting them into separate PRs to keep each review focused.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The dp array in the Unique BST solution is declared as size 20 but needs to support indices up to n=20, so increase it to at least dp[21] (or use a vector) to avoid out-of-bounds access.
- Use a 64-bit type (long long or uint64_t) for Catalan numbers when n can be 20, since Catalan(20) exceeds 32-bit integer range and will overflow.
- This PR currently includes solutions for both 'Candy' and 'Unique Binary Search Trees'—consider splitting them into separate PRs to keep each review focused.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
4 tasks
Owner
kaif969
commented
Oct 30, 2025
Contributor
Author
kaif969
left a comment
There was a problem hiding this comment.
@SjxSubham delete the other two files. Please check.
SjxSubham
approved these changes
Oct 30, 2025
🎉 Congrats on getting your PR merged in, @kaif969! 🙌🏼Thanks for your contribution every effort helps improve the project. Looking forward to seeing more from you! 🥳✨ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR Title Format: 96. Unique Binary Search Trees.cpp
Intuition
Choose any value i (1 ≤ i ≤ N) as root. Values
[1..i-1]must go to the left subtree (size i-1) and values[i+1..N]to the right subtree (size N-i). If G(n) is the number of unique BSTs with n nodes, then for a fixed root i there are G(i-1) * G(N-i) distinct BSTs (left and right subtrees combine independently). Summing over all possible roots yields the recurrence:G(N) = sum_{i=1..N} G(i-1) * G(N-i)
Base cases: G(0) = 1 (empty tree), G(1) = 1.
This is the Catalan-number recurrence. A DP solution computes
G[0..N]bottom-up (or uses memoized recursion) in O(N^2) time and O(N) space. Example: for N = 3,G(3) = G(0)G(2) + G(1)G(1) + G(2)G(0) = 2 + 1 + 2 = 5.
Approach
We solve this using the Catalan-number DP recurrence with two equivalent implementation styles: top-down memoized recursion or bottom-up iteration.
Initialization
n ≤ 20. Initializedp[0] = 1anddp[1] = 1.Recurrence
dp[n] = sum_{i=1..n} dp[i-1] * dp[n-i]Top-down (memoized recursion)
dp[n]is already computed, return it (avoids repeated work).dp[n]by looping i=1..n and accumulatingnumTrees(i-1) * numTrees(n-i)where recursive calls fill smaller dp entries.dp[n].Bottom-up (iterative DP)
2..n: for root=1..nodes: dp[nodes] += dp[root-1] * dp[nodes-root]Complexity
Code Solution (C++)
Related Issues
#259
By submitting this PR, I confirm that:
Summary by Sourcery
Introduce C++ DP-based solutions for the Candy and Unique Binary Search Trees problems on LeetCode.
New Features: