码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode-95. Unique Binary Search Trees II

时间:2016-02-21 14:22:14      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:

Description:

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

和第I题的区别是这题要返回所有的结果,不是结果的数目。所以要递归枚举:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    
    public List<TreeNode> generate(int start, int end) {
        List<TreeNode> ret = new ArrayList<TreeNode>();
        
        if(start > end) {
            ret.add(null);
            return ret;
        }
        
        for(int i=start; i<=end; i++) {
            List<TreeNode> lTree = generate(start, i-1); //生成左子树集合
            List<TreeNode> rTree = generate(i+1, end);  //生成右子树集合
            for(int j=0; j<lTree.size(); j++) {
                for(int k=0; k<rTree.size(); k++) {
                    TreeNode node = new TreeNode(i+1);  //连接到根节点
                    node.left = lTree.get(j);
                    node.right = rTree.get(k);
                    ret.add(node);
                }
            }
        }
        return ret;
    }
    
    public List<TreeNode> generateTrees(int n) {
        if(n <= 0) return new ArrayList<TreeNode>();
        return generate(0, n-1);
    }
}

技术分享

C++:

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<TreeNode *> generate(int start, int end)
    {
        vector<TreeNode* > ret;
        if (start > end)
        {
            ret.push_back(NULL);
            return ret;
        }
        
        for(int i = start; i <= end; i++)
        {
            vector<TreeNode* > lTree = generate(start, i - 1);
            vector<TreeNode* > rTree = generate(i + 1, end);
            for(int j = 0; j < lTree.size(); j++)
                for(int k = 0; k < rTree.size(); k++)
                {
                    TreeNode *node = new TreeNode(i + 1);
                    ret.push_back(node);
                    node->left = lTree[j];
                    node->right = rTree[k];              
                }           
        }
        
        return ret;
    }
    
    vector<TreeNode *> generateTrees(int n) {
        vector<TreeNode* > ret;
        if(n <= 0) return ret;
        return generate(0, n - 1);
    }
};

技术分享

同样的代码同样的测试数据Java代码为什么效率高这么多,难道是跑代码的服务器不一样?还是Java后台调用代码写的好?

 

LeetCode-95. Unique Binary Search Trees II

标签:

原文地址:http://www.cnblogs.com/wxisme/p/5204828.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!