LeetCode 刷题记录 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
解法:
递归法:
在这里插入图片描述
1 … n.的BST可以根据1到n为根来分
如n为3 可以分为根为1、根为2和根为3
同时根的左子树的数字比根小,根的右子树的数字比根大
递归函数有两个参数start和end,初始值为1和n
假设n = 3 start和end,初始值为1和3
从start到end,遍历所有根的情况
如i = 1,则递归的求它的左子树left[1,0]和右子树right[2,3]
获取到树后我们就可以生成BST
首先我们生成根节点i
然后我们先安左子树,左子树可能不止一颗,在确定左子树后然后再依次安右子树,最后将整个树加入结果集
如果start > end,说明是空树,直接加入空指针即可
左子树left[1,0] ,说明是空树
右子树right[2,3],我们同样遍历2和3
根为 2 左子树left[2,1]和右子树right[3,3]
左子树left[2,1],说明是空树
右子树right[3,3],只有一个节点3
根为 3 左子树left[2,2]和右子树right[4,3]
左子树left[2,1],只有一个节点2
右子树right[4,3],说明是空树
c++;

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<TreeNode*> generateTrees(int n) {
        if(n==0) return {};
        return dfs(1,n);
    }
    vector<TreeNode*> dfs(int start,int end) {
        vector<TreeNode*> res;
        if(start > end ){
            res.push_back(nullptr);
        }
        for(int i = start; i <= end; i++){
            vector<TreeNode*> left = dfs(start,i-1);
            vector<TreeNode*> right = dfs(i+1,end);
            for(auto a : left){
                for(auto b : right){
                    TreeNode* node = new TreeNode(i);
                    node->left = a;
                    node->right = b;
                    res.push_back(node);
                }
            }
        }
        
        return res;
    }
};

java:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public List<TreeNode> generateTrees(int n) {
        List<TreeNode> res = new ArrayList<>();
        if(n==0) return res;
        return dfs(1,n);
    }
    public List<TreeNode> dfs(int start,int end) {
        List<TreeNode> res = new ArrayList<>();
        if(start > end ){
            res.add(null);
        }
        for(int i = start; i <= end; i++){
            List<TreeNode> left = dfs(start,i-1);
            List<TreeNode> right = dfs(i+1,end);
            for(TreeNode a : left){
                for(TreeNode b : right){
                    TreeNode node = new TreeNode(i);
                    node.left = a;
                    node.right = b;
                    res.add(node);
                }
            }
        }
        
        return res;
    }
}

python:

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def generateTrees(self, n):
        """
        :type n: int
        :rtype: List[TreeNode]
        """
        res = []
        if n==0: return res
        return self.dfs(1,n);
    def dfs(self,start,end):
        res = []
        if start > end:
            res.append(None)
        
        for i in range(start,end+1):
            left = self.dfs(start,i-1)
            right = self.dfs(i+1,end)
            for a in left:
                for b in  right:
                    node = TreeNode(i)
                    node.left = a
                    node.right = b
                    res.append(node)
                
            

        return res
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值