给你一个整数 n
,请你生成并返回所有由 n
个节点组成且节点值从 1
到 n
互不相同的不同 二叉搜索树 。可以按 任意顺序 返回答案。
输入:
n = 3
输出:
[[1,null,2,null,3],[1,null,3,2],[2,1,3],[3,1,null,null,2],[3,2,null,1]]
输入:
n = 1
输出:
[[1]]
1
到 n
。// 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::rc::Rc;
use std::cell::RefCell;
impl Solution {
pub fn generate_trees(n: i32) -> Vec<Option<Rc<RefCell<TreeNode>>>> {
fn generate_trees(start: i32, end: i32) -> Vec<Option<Rc<RefCell<TreeNode>>>> {
let mut all_trees = Vec::new();
if start > end {
all_trees.push(Option::None);
return all_trees;
}
// 枚举可行根节点
(start..=end).for_each(|i| {
// 获得所有可行的左子树集合
let left_trees = generate_trees(start, i - 1);
// 获得所有可行的右子树集合
let right_trees = generate_trees(i + 1, end);
// 从左子树集合中选出一棵左子树,从右子树集合中选出一棵右子树,拼接到根节点上
left_trees.iter().for_each(|left| {
right_trees.iter().for_each(|right| {
let mut curr_tree = TreeNode::new(i);
curr_tree.left = left.clone();
curr_tree.right = right.clone();
all_trees.push(Option::Some(Rc::new(RefCell::new(curr_tree))));
});
});
});
return all_trees;
}
if n == 0 {
return Vec::new();
}
return generate_trees(1, n);
}
}
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func generateTrees(n int) []*TreeNode {
var generateTrees func(int, int) []*TreeNode
generateTrees = func(start, end int) []*TreeNode {
if start > end {
return []*TreeNode{nil}
}
allTrees := []*TreeNode{}
// 枚举可行根节点
for i := start; i <= end; i++ {
// 获得所有可行的左子树集合
leftTrees := generateTrees(start, i-1)
// 获得所有可行的右子树集合
rightTrees := generateTrees(i+1, end)
// 从左子树集合中选出一棵左子树,从右子树集合中选出一棵右子树,拼接到根节点上
for _, left := range leftTrees {
for _, right := range rightTrees {
currTree := &TreeNode{i, left, right}
allTrees = append(allTrees, currTree)
}
}
}
return allTrees
}
if n == 0 {
return nil
}
return generateTrees(1, n)
}
/**
* 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 {
private:
vector<TreeNode *> generateTrees(int start, int end) {
if (start > end) {
return {nullptr};
}
vector<TreeNode *> allTrees;
// 枚举可行根节点
for (int i = start; i <= end; ++i) {
// 获得所有可行的左子树集合
vector<TreeNode *> leftTrees = generateTrees(start, i - 1);
// 获得所有可行的右子树集合
vector<TreeNode *> rightTrees = generateTrees(i + 1, end);
// 从左子树集合中选出一棵左子树,从右子树集合中选出一棵右子树,拼接到根节点上
for (auto &left: leftTrees) {
for (auto &right: rightTrees) {
TreeNode *currTree = new TreeNode(i, left, right);
allTrees.emplace_back(currTree);
}
}
}
return allTrees;
}
public:
vector<TreeNode *> generateTrees(int n) {
if (!n) {
return {};
}
return generateTrees(1, n);
}
};
# 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 generate_trees(start, end):
if start > end:
return [None, ]
all_trees = []
for i in range(start, end + 1): # 枚举可行根节点
# 获得所有可行的左子树集合
leftTrees = generate_trees(start, i - 1)
# 获得所有可行的右子树集合
rightTrees = generate_trees(i + 1, end)
# 从左子树集合中选出一棵左子树,从右子树集合中选出一棵右子树,拼接到根节点上
for l in leftTrees:
for r in rightTrees:
curr_tree = TreeNode(i, l, r)
all_trees.append(curr_tree)
return all_trees
return generate_trees(1, n) if n else []
/**
* 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) {
if (n == 0) {
return new LinkedList<TreeNode>();
}
return generateTrees(1, n);
}
private List<TreeNode> generateTrees(int start, int end) {
List<TreeNode> allTrees = new LinkedList<TreeNode>();
if (start > end) {
allTrees.add(null);
return allTrees;
}
// 枚举可行根节点
for (int i = start; i <= end; ++i) {
// 获得所有可行的左子树集合
List<TreeNode> leftTrees = generateTrees(start, i - 1);
// 获得所有可行的右子树集合
List<TreeNode> rightTrees = generateTrees(i + 1, end);
// 从左子树集合中选出一棵左子树,从右子树集合中选出一棵右子树,拼接到根节点上
for (TreeNode left : leftTrees) {
for (TreeNode right : rightTrees) {
TreeNode currTree = new TreeNode(i, left, right);
allTrees.add(currTree);
}
}
}
return allTrees;
}
}