leetcode Unique Binary Search Trees II
来源:互联网 发布:网络计划的绘制规则 编辑:程序博客网 时间:2024/05/01 16:45
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.
The main problem is how to save the results from both left children and right children. But the answer is, there is no better solution than a vector for left children and a vector for right children. The following is 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: vector<TreeNode*> generate(vector<int>& node, int start, int end) { vector<TreeNode*> res, lchild, rchild; TreeNode *parent, *lc, *rc; if (start > end) { res.push_back(NULL); return res; } if (start == end) { parent = new TreeNode(node[start]); res.push_back(parent); return res; } else { for (int i = start; i <= end; i++) { lchild = generate(node, start, i-1); rchild = generate(node, i+1, end); for (int m = 0; m < lchild.size(); m++) for (int n = 0; n < rchild.size(); n++) { parent = new TreeNode(node[i]); parent->left = lchild[m]; parent->right = rchild[n]; res.push_back(parent); } } return res; } } vector<TreeNode *> generateTrees(int n) { // Note: The Solution object is instantiated only once and is reused by each test case. vector<TreeNode*> res; vector<int> node; for (int i=1; i <= n; ++i) node.push_back(i); res = generate(node, 0, n-1); return res; }};
After typing the code, I found there is no need to give the parameter node.
Give a WA Code here, find the bug:
/** * 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*> res; TreeNode *root = NULL; if (start > end) { res.push_back(NULL); return res; } if (start == end) { root = new TreeNode(start); res.push_back(root); return res; } for (int i = start; i <= end; ++i) { vector<TreeNode*> l = generate(start, i - 1); vector<TreeNode*> r = generate(i + 1, end); for (int j = 0; j < l.size(); ++j) for (int k = 0; k < r.size(); ++k) { root = new TreeNode(i); root->left = l[j]; root->right = r[k]; res.push_back(root); } } return res; } vector<TreeNode*> generateTrees(int n) { return generate(1, n); }};
- LeetCode: 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
- [leetcode]Unique Binary Search Trees II
- LeetCode-Unique Binary Search Trees II
- [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] 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
- Leetcode Unique Binary Search Trees II
- [LeetCode] Unique Binary Search Trees II
- Xcode崩溃、内存泄露检查方法
- 项目经理怎样给领导汇报工作?
- centOS yum 安装mysql
- Java集合框架List,Map,Set等全面介绍
- 感慨以及牢骚
- leetcode Unique Binary Search Trees II
- Oracle 10g 中的递归查询(树型查询)
- linux 本地通信实例 AF_UNIX close (sockfd)很重要
- 关键词推荐工具中的用户引导机制之一
- 【Android开发】Android开发环境搭建----第一个Android程序Hello World(上)
- 开源代码最近20年的可视化历史
- 一个不错的素材网站-千图网
- 关键词推荐工具中的用户引导机制之二
- Oracle EBS R12 AP模块主要表结构整理