leetcode95 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.
思路:
本题采取递归的思路。
传递的参数是开始数值(begin)和结束数值(end)。
当begin > end 时,返回空(注意不是null);
当begin == end 时, 返回含有 new TreeNode(begin)结点的ArrayList;
当begin < end时,建立两个ArrayList来分别接收左右子树。
代码:
- public List<TreeNode> generateTrees(int n){
- return generateTrees(1, n);
- }
- public List<TreeNode> generateTrees(int begin, int end){
- List<TreeNode> arr = new ArrayList<TreeNode>();
- if(begin > end){
- return arr;
- }
- if(begin == end){
- TreeNode ptr = new TreeNode(begin);
- arr.add(ptr);
- return arr;
- }
- for(int i = begin; i <= end; i++){
- List<TreeNode> left = new ArrayList<TreeNode>();
- List<TreeNode> right = new ArrayList<TreeNode>();
- left = generateTrees(begin, i-1);
- right = generateTrees(i+1, end);
- //注意判断left和right是否为空
- //还有,要注意应该在最内层循环每次都新建根结点
- if(left.size() == 0){
- if(right.size() == 0){
- TreeNode root = new TreeNode(i);
- root.left = null;
- root.right = null;
- arr.add(root);
- }else{
- for(TreeNode r: right){
- TreeNode ptr = new TreeNode(i);
- ptr.left = null;
- ptr.right = r;
- arr.add(ptr);
- }
- }
- }else{
- if(right.size() == 0){
- for(TreeNode l: left){
- TreeNode ptr = new TreeNode(i);
- ptr.left = l;
- ptr.right = null;
- arr.add(ptr);
- }
- }else{
- for(TreeNode l: left){
- for(TreeNode r: right){
- TreeNode ptr = new TreeNode(i);
- ptr.left = l;
- ptr.right = r;
- arr.add(ptr);
- }
- }
- }
- }
- }
- return arr;
- }
leetcode95 Unique Binary Search Trees II的更多相关文章
- LeetCode-95. Unique Binary Search Trees II
Description: Given n, generate all structurally unique BST's (binary search trees) that store values ...
- Leetcode95. Unique Binary Search Trees II不同的二叉搜索树2
给定一个整数 n,生成所有由 1 ... n 为节点所组成的二叉搜索树. 示例: 输入: 3 输出: [ [1,null,3,2], [3,2,null,1], [3,1,null,nul ...
- 【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 ...
随机推荐
- egrep 查找IP
1. egrep '([^0-9]|\<)(([0-1]?[0-9]{0,2}|([2]([0-4][0-9]|[5][0-5])))\.){3}([0-1]?[0-9]{0,2}|([2]([ ...
- Delphi 如何清除动态数组的内存?
SetLength(glb_IndexConfig,); FreeAndNil(glb_IndexConfig);
- common.js
//检测浏览器 function checkb(){ var Sys = {}; var ua = navigator.userAgent.toLowerCase(); var s; (s = ua. ...
- JS使用ActiveXObject读取数据库代码示例(只支持IE)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- yii2 rbac-plus的使用
前言 1.本教程适合有RBAC基础,对RBAC有一定了解的同学. 2.本教程使用advanced模板 3.确保数据库中存在user表,没有的同学请查阅文档 运行 php yii migrate 来生成 ...
- iOS新建项目文件管理规范
当我们进入到新的公司的第一天,看到以前老员工编写的代码,找个东西累死人咧,那个抓耳挠腮的啊,一般情况下都有想揍人的赶脚. 哈哈,不忙,先想一下自己的代码!想一下自己写的代码怎么才能新来的人一眼就能看懂 ...
- php+mysql实现事务回滚
模拟条件:第一个表插入成功,但是第二个表插入失败,回滚.第一个表插入成功,第二个表插入成功,执行.第一个表插入失败,第二个表插入成功,回滚.第一个表插入失败,第二个表插入失败,回滚.以上情况都需要回滚 ...
- angularJS自定义属性作为条件中转
<html> <head> <meta charset="utf-8"/> <title></title> </h ...
- [LeetCode] Combinations (bfs bad、dfs 递归 accept)
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- 实验 snort安装配置与规则编写
1 实验目的 在linux或windows任意一个平台下完成snort的安装,使snort工作在NIDS模式下,并编写符合相关情景要求的snort规则. 2 实验环境 物理机:windows 8.1 ...