Unique Binary Search Trees Solved with Recursion and Memoization

Day 70 of #100DaysOfCode Today I solved "Unique Binary Search Trees" on LeetCode using Recursion + Dynamic Programming (Memoization). Key Idea: For n nodes, we try every value as the root. If a value root is chosen: Left subtree can be formed using (root − 1) nodes Right subtree can be formed using (n − root) nodes The total number of BSTs for that root becomes: left_subtrees × right_subtrees By summing this for all possible roots, we get the total number of unique BSTs. To avoid recomputing the same subproblems, I used memoization. Concepts Used: • Recursion • Dynamic Programming • Memoization • Binary Search Tree properties Time Complexity: O(n²) Space Complexity: O(n) This problem is a classic example of the Catalan Number pattern in dynamic programming. Step by step, building stronger intuition for DP and tree-based problems. #100DaysOfCode #DSA #LeetCode #DynamicProgramming #BinaryTree #Cpp #CodingJourney

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories