leetcode

package
v0.0.0-...-25c03cf Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 22, 2024 License: MIT Imports: 1 Imported by: 0

README

95. Unique Binary Search Trees II

题目

Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ... n.

Example:

Input: 3
Output:
[
  [1,null,3,2],
  [3,2,null,1],
  [3,1,null,null,2],
  [2,1,3],
  [1,null,2,null,3]
]
Explanation:
The above output corresponds to the 5 unique BST's shown below:

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

题目大意

给定一个整数 n,生成所有由 1 ... n 为节点所组成的二叉搜索树。

解题思路

  • 输出 1n 元素组成的 BST 所有解。这一题递归求解即可。外层循环遍历 1n 所有结点,作为根结点,内层双层递归分别求出左子树和右子树。

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type TreeNode

type TreeNode = structures.TreeNode

TreeNode define

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL