{ "data": { "question": { "questionId": "95", "questionFrontendId": "95", "categoryTitle": "Algorithms", "boundTopicId": 1422, "title": "Unique Binary Search Trees II", "titleSlug": "unique-binary-search-trees-ii", "content": "
Given an integer n
, return all the structurally unique BST's (binary search trees), which has exactly n
nodes of unique values from 1
to n
. Return the answer in any order.
\n
Example 1:
\n\n\nInput: n = 3\nOutput: [[1,null,2,null,3],[1,null,3,2],[2,1,3],[3,1,null,null,2],[3,2,null,1]]\n\n\n
Example 2:
\n\n\nInput: n = 1\nOutput: [[1]]\n\n\n
\n
Constraints:
\n\n1 <= n <= 8
给你一个整数 n
,请你生成并返回所有由 n
个节点组成且节点值从 1
到 n
互不相同的不同 二叉搜索树 。可以按 任意顺序 返回答案。
\n\n
示例 1:
\n\n\n输入:n = 3\n输出:[[1,null,2,null,3],[1,null,3,2],[2,1,3],[3,1,null,null,2],[3,2,null,1]]\n\n\n
示例 2:
\n\n\n输入:n = 1\n输出:[[1]]\n\n\n
\n\n
提示:
\n\n1 <= n <= 8