Unique Binary Search Trees II

本文介绍了一种使用递归方法生成所有可能的唯一二叉搜索树(BST)的算法,并提供了一个改进版本以解决原始方法中出现的超时问题。通过递归构造核心函数,能够有效地生成指定范围内的所有唯一BST。

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

For example,
Given n = 3, your program should return all 5 unique BST's shown below.

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

递归方法超时。

/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; left = null; right = null; }
 * }
 */
public class Solution {
    public List<TreeNode> generateTrees(int n) {
        List<TreeNode> res=new LinkedList<TreeNode>();
        if(n<=0) return res;
        boolean []vis=new boolean[n+1];
        for(int i=1;i<=n;i++){
            TreeNode tree=new TreeNode(i);
            vis[i]=true;
            buildTree(1,i,n,vis,tree,res);
            vis[i]=false;
        }
        return res;
    }
    public void buildTree(int size,int curr,int n,boolean []vis,TreeNode tree,List<TreeNode> res){
        if(size==n){
            TreeNode t=tree;
            res.add(t);
            return;
        }
        for(int i=1;i<=n;i++){
            if(!vis[i]){
                vis[i]=true;
                if(i>curr){
                    tree.right=new TreeNode(i);
                    buildTree(size+1,i,n,vis,tree.right,res);
                    tree.right=null;
                }
                else if(i<curr){
                    tree.left=new TreeNode(i);
                    buildTree(size+1,i,n,vis,tree.left,res);
                    tree.left=null;
                }
            }
            vis[i]=false;
        }
    }
}

改进版

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值