Files
LeetCode-Go/website/content/ChapterFour/0095.Unique-Binary-Search-Trees-II.md
halfrost 46faa66642 Merge pull request #60 from halfrost/code_quality_improvement
optimization code quality level from A to A+
2020-08-27 17:41:27 +08:00

78 lines
1.6 KiB
Markdown
Executable File
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# [95. Unique Binary Search Trees II](https://leetcode.com/problems/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 为节点所组成的二叉搜索树。
## 解题思路
- 输出 1~n 元素组成的 BST 所有解。这一题递归求解即可。外层循环遍历 1~n 所有结点,作为根结点,内层双层递归分别求出左子树和右子树。
## 代码
```go
package leetcode
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func generateTrees(n int) []*TreeNode {
if n == 0 {
return []*TreeNode{}
}
return generateBSTree(1, n)
}
func generateBSTree(start, end int) []*TreeNode {
tree := []*TreeNode{}
if start > end {
tree = append(tree, nil)
return tree
}
for i := start; i <= end; i++ {
left := generateBSTree(start, i-1)
right := generateBSTree(i+1, end)
for _, l := range left {
for _, r := range right {
root := &TreeNode{Val: i, Left: l, Right: r}
tree = append(tree, root)
}
}
}
return tree
}
```