96. Unique Binary Search Trees

Source code notebook Author Update time

Given n , how many structurally unique BST 's (binary search trees) that store values 1 ... n?

Example:

Input: 3
Output: 5
Explanation: Given _n_ = 3, there are a total of 5 unique BST's:

   1         3     3      2      1
    \       /     /      / \      \
     3     2     1      1   3      2
    /     /       \                 \
   2     1         2                 3

Constraints:

  • 1 <= n <= 19
# @lc code=start
using LeetCode

num_of_trees(n::Int) = binomial(2 * n, n) - binomial(2 * n, n + 1)
# @lc code=end
num_of_trees (generic function with 1 method)

This page was generated using DemoCards.jl and Literate.jl.