LeetCode题解——Unique Binary Search Trees II
2015-11-07 22:45
495 查看
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.
想法一:递归,思想见Unique Binary Search Trees
思想二:DP ;
The basic idea is that we can construct the result of n node tree just from the result of n-1 node tree. Here's how we do it: only 2
conditions: 1) The nth node is the new root, so
the nth node is not root, we traverse the old tree, every time the node in the old tree has a right child, we can perform:
nth node here. One thing to notice is that every time we push a TreeNode in our result, I push the clone version of the root, and I recover what I do to the old node immediately. This is because you may use the old tree for several times.
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
想法一:递归,思想见Unique Binary Search Trees
class Solution { public: vector<TreeNode*> generateTrees(int n) { return generateTrees(1,n); } vector<TreeNode*> generateTrees(int i, int j) {//建立sequence i,i+1,...,j的BST vector<TreeNode*> temp; TreeNode* root=NULL; if(i>j){ temp.push_back(root); return temp; } if(i==j){ root = new TreeNode(i); temp.push_back(root); return temp; } for(int m = i;m<=j;m++){ vector<TreeNode*> left = generateTrees(i,m-1); vector<TreeNode*> right = generateTrees(m+1,j); for(int l = 0;l<left.size();l++){ for(int r = 0; r<right.size();r++){ TreeNode* root = new TreeNode(m); root->left = left[l]; root->right = right[r]; temp.push_back(root); } } } return temp; } };
思想二:DP ;
The basic idea is that we can construct the result of n node tree just from the result of n-1 node tree. Here's how we do it: only 2
conditions: 1) The nth node is the new root, so
newroot->left = oldroot;2)
the nth node is not root, we traverse the old tree, every time the node in the old tree has a right child, we can perform:
old node->right = nth node, nth node ->left = right child;and when we reach the end of the tree, don't forget we can also add the
nth node here. One thing to notice is that every time we push a TreeNode in our result, I push the clone version of the root, and I recover what I do to the old node immediately. This is because you may use the old tree for several times.
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { private: TreeNode * clone(TreeNode * root){ if(NULL == root) return NULL; TreeNode * newRoot = new TreeNode(root->val); newRoot->left = clone(root->left); newRoot->right = clone(root->right); return newRoot; } void addToLeft(TreeNode * oldRoot, TreeNode * newRoot, vector<TreeNode *> & results){ TreeNode * cloneRoot = clone(oldRoot); newRoot->left = cloneRoot; results.push_back(newRoot); } void addRight(TreeNode * oldRoot, TreeNode * cur, TreeNode * newNode, vector<TreeNode *> & results){ TreeNode *cloneRoot = clone(oldRoot); TreeNode *newCur = cloneRoot; while(NULL != newCur){ if(newCur->val == cur->val) break; else newCur = newCur->right; } assert(NULL != newCur); TreeNode * temp = newCur->right; newCur->right = newNode; newNode->left = temp; results.push_back(cloneRoot); } public: vector<TreeNode *> generateTrees(int n) { // DP: // for n-1 to n , for each n -1 // 1) left to n // 2) for each right child down // n replace right and right will be n left vector<TreeNode *> results; vector<TreeNode *> preResults(1, NULL); for(int i = 1; i <=n; i ++){ for(int j = 0; j < preResults.size(); j++){ // add n-1 left to n TreeNode * oldRoot = preResults[j]; TreeNode * newRoot = new TreeNode(i); addToLeft(oldRoot, newRoot, results); TreeNode * cur = oldRoot; while(NULL != cur){ TreeNode *newNode = new TreeNode(i); addRight(oldRoot, cur, newNode, results); cur = cur->right; } } swap(results, preResults); results.clear(); } return preResults; } };
相关文章推荐
- UEFI下装Ubuntu Win10 双系统
- 【译】RabbitMQ:工作队列(Work Queue)
- IOS UIFont
- IOS开发UI篇--使用UICollectionView实现一个无限轮播的案例
- Android 系统UI状态栏设置
- java String / StringBuilder / StringBuffer 的使用
- Machine Learning week 1 quiz: Linear Algebra
- Machine Learning week 1 quiz: Linear Regression with One Variable
- UIDate详解
- Machine Learning week 1 quiz: Linear Regression with One Variable
- 【Codeforces Round 328 (Div 2)B】【找规律】The Monster and the Squirrel 正多边形连边分割块数
- uilongPressGestureRecognizer 的两次(多次)触发
- Machine Learning week 1 quiz: Introduction
- IOS7以上的UITableView顶部空白的问题
- [ecshop调试]ecshop 数据库查询缓存详解 有三种缓存,query_cache(数据库查询缓存)、static_cache(静态缓存)和cache(普通的缓存)
- 去掉 UITableView 顶部的多余空间
- [POJ1679]The Unique MST 次小生成树
- IOS UITextView
- 模仿UITableView的WaterFlowView和控件的懒加载
- 解析XML--用php,dom来获得xml文件里的标签内value