Unique Binary Search Trees II(leetcode)
2014-11-03 20:01
288 查看
题目:
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.
confused what
read more on how binary tree is serialized on OJ.
OJ's Binary Tree Serialization:
The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.
Here's an example:
The above binary tree is serialized as
解题思路:递归,参考了leetcode题解。调试时不需要把信息打印输出,只需要在return result处加一个断点,就能查看result的值了。这样能够避免写过多的调试代码。
代码:
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
confused what
"{1,#,2,3}"means? >
read more on how binary tree is serialized on OJ.
OJ's Binary Tree Serialization:
The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.
Here's an example:
1 / \ 2 3 / 4 \ 5
The above binary tree is serialized as
"{1,2,3,#,#,4,#,#,5}".
解题思路:递归,参考了leetcode题解。调试时不需要把信息打印输出,只需要在return result处加一个断点,就能查看result的值了。这样能够避免写过多的调试代码。
代码:
#include<iostream> #include<vector> using namespace std; struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; vector<TreeNode *> generateTrees(int n) { if(n==0) return generate(1,0); return generate(1,n); } vector<TreeNode *> generate(int start,int end) { vector<TreeNode *> result; if(start>end) { result.push_back(nullptr); return result; } for(int k=start;k<=end;k++) { vector<TreeNode*> succLeft=generate(start,k-1); vector<TreeNode*> succRight=generate(k+1,end); for(auto i=succLeft.begin();i!=succLeft.end();i++) { for(auto j=succRight.begin();j!=succRight.end();j++) { TreeNode *node=new TreeNode(k); node->left=*i; node->right=*j; result.push_back(node); } } } return result; } int main() { generateTrees(5); system("pause"); return 0; }
相关文章推荐
- [leetcode] 95.Unique Binary Search Trees II
- leetcode - Unique Binary Search Trees II
- [leetcode] Unique Binary Search Trees II
- leetcode — unique-binary-search-trees-ii
- LeetCode: Unique Binary Search Trees II [096]
- LeetCode Unique Binary Search Trees II
- LeetCode | Unique Binary Search Trees II
- [leetcode]95. Unique Binary Search Trees II
- 【Leetcode】Unique Binary Search Trees II
- [leetcode-95]Unique Binary Search Trees II(c++)
- Leetcode-95.Unique Binary Search Trees II(a representative recursion problem)
- 【LEETCODE】95-Unique Binary Search Trees II
- leetcode先刷_Unique Binary Search Trees II
- [Leetcode]Unique Binary Search Trees II
- [LeetCode]95.Unique Binary Search Trees II
- 095 Unique Binary Search Trees II [Leetcode]
- LeetCode 95. Unique Binary Search Trees II
- leetcode - Unique Binary Search Trees II
- [LeetCode] Unique Binary Search Trees II
- 【leetcode】Unique Binary Search Trees II