lintcode--不同的二叉查找树II
来源:互联网 发布:扫雷技巧红包软件 编辑:程序博客网 时间:2024/05/20 07:58
给出n,生成所有由1...n为节点组成的不同的二叉查找树
样例
给出n = 3,生成所有5种不同形态的二叉查找树:
1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ \2 1 2 3
/** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * this.left = this.right = null; * } * } *///博客 //组成不同的二叉查找树public class Solution { public List<TreeNode> generateTrees(int n) { return generate(1,n); } private ArrayList<TreeNode> generate(int start, int end) { ArrayList<TreeNode> rst = new ArrayList<TreeNode>(); if(start > end) { rst.add(null); return rst; } for(int i=start; i<=end; i++) { ArrayList<TreeNode> left = generate(start, i-1); ArrayList<TreeNode> right = generate(i+1, end); for(TreeNode l: left) { for(TreeNode r: right) { TreeNode root = new TreeNode(i); root.left = l; root.right = r; rst.add(root); } } } return rst; }}
阅读全文
1 0
- LintCode-不同的二叉查找树 II
- 不同的二叉查找树 II lintcode
- 不同的二叉查找树 II -LintCode
- lintcode--不同的二叉查找树II
- **[Lintcode]Unique Binary Search Trees II 不同的二叉查找树 II
- LintCode-不同的二叉查找树
- lintcode-不同的二叉查找树-163
- LintCode 不同的二叉查找树
- LintCode:不同的二叉查找树
- lintcode,不同的二叉查找树
- LintCode 163-不同的二叉查找树
- LintCode :不同的二叉查找树
- lintcode-不同的二叉查找树
- 不同的二叉查找树-LintCode
- lintcode --不同的二叉查找树
- lintcode:不同的二叉查找树
- 题目:不同的二叉查找树 II
- 不同的二叉查找树 II
- C3伪类选择器
- Github 入门
- 字符集和字符编码(Charset & Encoding)
- ARM9-学习笔记(一)
- docker 如何批量删除镜像
- lintcode--不同的二叉查找树II
- 第四篇:类加载机制
- 使用pylint 规范python代码
- 微信群发,图文消息
- 面试题:自己编写双向链表(不限语言)
- 自定义自适应的TabLayout
- 简单的饼状图
- 闭包
- MySQLdayday05(创建个表 数据类型 默认编码表 默认值 增删改 约束 )