由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - Unique Binary Search Trees II的复杂度怎么算啊?多谢!
相关主题
UNIQUE二叉搜树2查找binary tree中有多少个uni-valued subtree
这个Binary Tree的题来看看问一道google面经
请教find number of duplicates in a binary search treelargest bst 解法不理解的地方
Find the node with given value in binary tree in in-order贡献一个最近电面题目
Unique Binary Search Trees的变形Amazon Interview: algorithm for 2*LOG(N) up bound for search
求教balanced binary tree的准确定义问个binary search tree的问题
报google nyc offer,并分享面经上周Juniper onsite面经
请教这么一个题:BST maximum sum pathrecovery BST 不考虑相同值的情况么?
相关话题的讨论汇总
话题: treenode话题: generate话题: vector话题: subtree话题: return
进入JobHunting版参与讨论
1 (共1页)
a***e
发帖数: 413
1
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
这种题如果没见过我很难想出来,看了答案也不知道复杂度怎么算。汗, 现在在国内
还惦记着这些题。
class Solution {
public:
vector generateTrees(int n) {
if (n==0) return generate(1,0);
return generate(1,n);
}

private:
vector generate(int start, int end){
vector subTree;
if (start>end){
subTree.push_back(NULL);
return subTree;
}

for (int k=start; k<=end; k++){
vector leftSubs = generate(start, k-1);
vector rightSubs = generate(k+1, end);

for (auto i: leftSubs){
for (auto j:rightSubs){
TreeNode *node = new TreeNode(k);
node->left = i;
node->right = j;
subTree.push_back(node);
}
}

}

return subTree;
}
};
1 (共1页)
进入JobHunting版参与讨论
相关主题
recovery BST 不考虑相同值的情况么?Unique Binary Search Trees的变形
rebuild a tree from inorder and level order求教balanced binary tree的准确定义
问道150上的题:sum of path in binary tree报google nyc offer,并分享面经
Create Binary Tree from preorder and inorder arrays请教这么一个题:BST maximum sum path
UNIQUE二叉搜树2查找binary tree中有多少个uni-valued subtree
这个Binary Tree的题来看看问一道google面经
请教find number of duplicates in a binary search treelargest bst 解法不理解的地方
Find the node with given value in binary tree in in-order贡献一个最近电面题目
相关话题的讨论汇总
话题: treenode话题: generate话题: vector话题: subtree话题: return