树
二叉搜索树
动态规划
回溯
二叉树
题目描述
给你一个整数 n
,请你生成并返回所有由 n
个节点组成且节点值从 1
到 n
互不相同的不同 二叉搜索树 。可以按 任意顺序 返回答案。
示例 1:
输入: n = 3
输出: [[1,null,2,null,3],[1,null,3,2],[2,1,3],[3,1,null,null,2],[3,2,null,1]]
示例 2:
输入: n = 1
输出: [[1]]
提示:
解法
方法一:DFS
我们设计一个函数 $dfs(i, j)$,返回由 $[i, j]$ 组成的所有可行的二叉搜索树,那么答案就是 $dfs(1, n)$。
函数 $dfs(i, j)$ 的执行步骤如下:
如果 $i > j$,那么说明此时没有数字可以构成二叉搜索树,返回由一个空节点组成的列表。
如果 $i \leq j$,那么我们枚举 $[i, j]$ 中的数字 $v$ 作为根节点,那么根节点 $v$ 的左子树由 $[i, v - 1]$ 组成,右子树由 $[v + 1, j]$ 组成,最后将左右子树的所有组合笛卡尔积,即 $left \times right$,加上根节点 $v$,得到以 $v$ 为根节点的所有二叉搜索树。
时间复杂度 $O(n \times G(n))$,空间复杂度 $O(n \times G(n))$。其中 $G(n)$ 是卡特兰数。
Python3 Java C++ Go TypeScript Rust
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 # Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution :
def generateTrees ( self , n : int ) -> List [ Optional [ TreeNode ]]:
def dfs ( i : int , j : int ) -> List [ Optional [ TreeNode ]]:
if i > j :
return [ None ]
ans = []
for v in range ( i , j + 1 ):
left = dfs ( i , v - 1 )
right = dfs ( v + 1 , j )
for l in left :
for r in right :
ans . append ( TreeNode ( v , l , r ))
return ans
return dfs ( 1 , n )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List < TreeNode > generateTrees ( int n ) {
return dfs ( 1 , n );
}
private List < TreeNode > dfs ( int i , int j ) {
List < TreeNode > ans = new ArrayList <> ();
if ( i > j ) {
ans . add ( null );
return ans ;
}
for ( int v = i ; v <= j ; ++ v ) {
var left = dfs ( i , v - 1 );
var right = dfs ( v + 1 , j );
for ( var l : left ) {
for ( var r : right ) {
ans . add ( new TreeNode ( v , l , r ));
}
}
}
return ans ;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 /**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public :
vector < TreeNode *> generateTrees ( int n ) {
function < vector < TreeNode *> ( int , int ) > dfs = [ & ]( int i , int j ) {
if ( i > j ) {
return vector < TreeNode *> { nullptr };
}
vector < TreeNode *> ans ;
for ( int v = i ; v <= j ; ++ v ) {
auto left = dfs ( i , v - 1 );
auto right = dfs ( v + 1 , j );
for ( auto l : left ) {
for ( auto r : right ) {
ans . push_back ( new TreeNode ( v , l , r ));
}
}
}
return ans ;
};
return dfs ( 1 , n );
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 /**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func generateTrees ( n int ) [] * TreeNode {
var dfs func ( int , int ) [] * TreeNode
dfs = func ( i , j int ) [] * TreeNode {
if i > j {
return [] * TreeNode { nil }
}
ans := [] * TreeNode {}
for v := i ; v <= j ; v ++ {
left := dfs ( i , v - 1 )
right := dfs ( v + 1 , j )
for _ , l := range left {
for _ , r := range right {
ans = append ( ans , & TreeNode { v , l , r })
}
}
}
return ans
}
return dfs ( 1 , n )
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 /**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/
function generateTrees ( n : number ) : Array < TreeNode | null > {
const dfs = ( i : number , j : number ) : Array < TreeNode | null > => {
if ( i > j ) {
return [ null ];
}
const ans : Array < TreeNode | null > = [];
for ( let v = i ; v <= j ; ++ v ) {
const left = dfs ( i , v - 1 );
const right = dfs ( v + 1 , j );
for ( const l of left ) {
for ( const r of right ) {
ans . push ( new TreeNode ( v , l , r ));
}
}
}
return ans ;
};
return dfs ( 1 , n );
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 // Definition for a binary tree node.
// #[derive(Debug, PartialEq, Eq)]
// pub struct TreeNode {
// pub val: i32,
// pub left: Option<Rc<RefCell<TreeNode>>>,
// pub right: Option<Rc<RefCell<TreeNode>>>,
// }
//
// impl TreeNode {
// #[inline]
// pub fn new(val: i32) -> Self {
// TreeNode {
// val,
// left: None,
// right: None
// }
// }
// }
use std :: cell :: RefCell ;
use std :: rc :: Rc ;
impl Solution {
pub fn generate_trees ( n : i32 ) -> Vec < Option < Rc < RefCell < TreeNode >>>> {
Self :: dfs ( 1 , n )
}
fn dfs ( i : i32 , j : i32 ) -> Vec < Option < Rc < RefCell < TreeNode >>>> {
let mut ans = Vec :: new ();
if i > j {
ans . push ( None );
return ans ;
}
for v in i ..= j {
let left = Self :: dfs ( i , v - 1 );
let right = Self :: dfs ( v + 1 , j );
for l in & left {
for r in & right {
ans . push ( Some ( Rc :: new ( RefCell :: new ( TreeNode {
val : v ,
left : l . clone (),
right : r . clone (),
}))));
}
}
}
ans
}
}