LeetCode_Unique Binary Search Trees II
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
转自: http://yucoding.blogspot.com/2013/05/leetcode-question115-unique-binary.html
Analysis:
The basic idea is still using the DFS scheme. It is a little hard to think the structure of the argument list in the function. It is clear that for each tree/subtree, we will set the root as the start position to the end position, and recursively construct the left subtree with the left part and the right subtree with the right part.
So first we can have
void dfs (int st, int ed ){
if (st>ed) { // generate a null node }
else{
for (int i=st;i<=ed;i++){
dfs(st,i-1, ); //generate left subtree
dfs(i+1,ed, ); // generate right subtree
}
}
}
Next step is to think about how to store all the possible solutions.
This is important ! Think about the root node, all the possible solutions of the tree are from the combinations of all the possible solutions of its left subtree, and its right subtree. One step further, if we have a root node and a left node, for the left node, still the subtrees below it are the combinations of the possible solutions of its left and right subtree, until the leaf node.
In other words, we store all the possible solutions for each node, instead of storing the only tree. So, we can have
void dfs(int st, int ed, vector<TreeNode *> res){}
in this function, recursively generate the left tree and right tree, then construct the current node, and push it to the current solution vector.
Details see the code.
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
void dfs(vector<TreeNode *> &res, int left , int right){
if(left > right){
res.push_back(NULL);
return ;
} for(int k = left; k <= right ; k++)
{
vector<TreeNode *> leftTree;
dfs(leftTree, left, k-);
vector<TreeNode *> rightTree;
dfs(rightTree, k+, right);
for(int i = ; i < leftTree.size(); i++)
for( int j = ; j < rightTree.size(); j++)
{
TreeNode *root = new TreeNode(k);
root->left = leftTree[i];
root->right = rightTree[j];
res.push_back(root);
}
}
}
vector<TreeNode *> generateTrees(int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<TreeNode *> res;
dfs(res, , n);
return res;
}
};
LeetCode_Unique Binary Search Trees II的更多相关文章
- 【LeetCode】95. Unique Binary Search Trees II
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
- 【leetcode】Unique Binary Search Trees II
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
- 41. Unique Binary Search Trees && Unique Binary Search Trees II
Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...
- LeetCode: Unique Binary Search Trees II 解题报告
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
- Unique Binary Search Trees,Unique Binary Search Trees II
Unique Binary Search Trees Total Accepted: 69271 Total Submissions: 191174 Difficulty: Medium Given ...
- [LeetCode] 95. Unique Binary Search Trees II(给定一个数字n,返回所有二叉搜索树) ☆☆☆
Unique Binary Search Trees II leetcode java [LeetCode]Unique Binary Search Trees II 异构二叉查找树II Unique ...
- LeetCode解题报告—— Reverse Linked List II & Restore IP Addresses & Unique Binary Search Trees II
1. Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass ...
- leetcode 96. Unique Binary Search Trees 、95. Unique Binary Search Trees II 、241. Different Ways to Add Parentheses
96. Unique Binary Search Trees https://www.cnblogs.com/grandyang/p/4299608.html 3由dp[1]*dp[1].dp[0]* ...
- 【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)
[LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...
随机推荐
- Keil C调试经验
我们使用Keil C调试某系统时积累的一些经验: 1.由于Keil C对中文支持不太好,因而会出现显示的光标与光标实际所在不一致的现象,这会对修改中文注释造成影响.在Windows2000下面 ...
- bzoj1188
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1188 一道非常好的SG函数题,加深了对博弈论的理解. 以前做的SG函数的题,都是每个瓶子看成一 ...
- 符号表实现(Symbol Table Implementations)
符号表的实现有很多方式,下面介绍其中的几种. 乱序(未排序)数组实现 这种情况,不需要改变数组,操作就在这个数组上执行.在最坏的情况下插入,搜索,删除时间复杂度为O(n). 有序(已排序)数组实现 这 ...
- 第33讲 UI组件_进度条ProcessBar和消息队列处理器handler
第33讲UI组件_进度条ProcessBar和消息队列处理器handler 1. 进度条ProcessBar 一个可视化的进度指示器,代表正在执行的耗时任务.可以为用户展示一个进度条,表示正在执行的任 ...
- windows 查看端口被占用
C:\Users\xxxx> 根据端口找到进程14716 C:\Users\xxxx>tasklist|findstr "14716"node.exe 14716 Co ...
- for的用法
第一次看到这么用,哈哈,就记下 for (var control = ["程", "陈", "是"]; control[0]; contro ...
- pyqt node节点1
#!/usr/bin/env python # coding: utf-8 from PyQt4.QtGui import * from PyQt4.QtCore import * rad = 5 c ...
- tab切换jquery代码
http://immmmm.com/jquery-tab-switch-code-improved.html html <div id="sidebar-tab"> ...
- (转)iOS7人机界面设计规范 - 目录
英文原文出自苹果官方的iOS7设计资源-iOS人机界面设计规范(预发布版本),由C7210自发翻译,并首发于Beforweb.com.如需转载,请注明译者及出处信息. UI设计基础 为iOS7而设计 ...
- Netty实例-简单的服务端-client实现,凝视具体
书籍推荐: 实例代码 :http://download.csdn.net/detail/jiangtao_st ...