[LeetCode]题解(python):095-Unique Binary Search Trees II
题目来源:
https://leetcode.com/problems/unique-binary-search-trees-ii/
题意分析:
给一个整数,返回所有中序遍历是1到n的树。
题目思路:
这可以用递归的思想。首先确定根节点,如果k是根节点,那么1-k-1是左子树,而k+1-n为右子树。
代码(python):
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def generateTrees(self, n):
"""
:type n: int
:rtype: List[TreeNode]
"""
if n == 0:
return []
def solve(begin,end):
if begin > end:
return [None]
i = begin
ans = []
while i <= end:
tmp1,tmp2 = solve(begin, i - 1),solve(i + 1,end)
for j in tmp1:
for k in tmp2:
tmp = TreeNode(i)
tmp.left = j
tmp.right = k
ans.append(tmp)
i += 1
return ans
return solve(1,n)
[LeetCode]题解(python):095-Unique Binary Search Trees II的更多相关文章
- Java for LeetCode 095 Unique Binary Search Trees II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- LeetCode(95) Unique Binary Search Trees II
题目 Given n, generate all structurally unique BST's (binary search trees) that store values 1-n. For ...
- 095 Unique Binary Search Trees II 不同的二叉查找树 II
给出 n,问由 1...n 为节点组成的不同的二叉查找树有多少种?例如,给出 n = 3,则有 5 种不同形态的二叉查找树: 1 3 3 2 1 ...
- 【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)
[LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...
- [LeetCode] 95. Unique Binary Search Trees II(给定一个数字n,返回所有二叉搜索树) ☆☆☆
Unique Binary Search Trees II leetcode java [LeetCode]Unique Binary Search Trees II 异构二叉查找树II Unique ...
- 【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) ...
- LeetCode: Unique Binary Search Trees II 解题报告
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
- 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 ...
- leetcode 96. Unique Binary Search Trees 、95. Unique Binary Search Trees II 、241. Different Ways to Add Parentheses
96. Unique Binary Search Trees https://www.cnblogs.com/grandyang/p/4299608.html 3由dp[1]*dp[1].dp[0]* ...
随机推荐
- City Game(动态规划)
City Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- S3C2416裸机开发系列十六_sd卡驱动实现
S3C2416裸机开发系列十六 sd卡驱动实现 象棋小子 1048272975 SD卡(Secure Digital Memory Card)具有体积小.容量大.传输数据快.可插拔.安全性好等长 ...
- [转]PB 基本语句 循环语句
PB 基本语句一.赋值语句赋值语句用于给变量.对象属性赋值,这是应用程序中使用最频繁的语句,其语法格式为:variablename = expression_r其中:⑴variablename是变量名 ...
- 获取 web容器中的bean
public class WebContextBeanFinder { public static Object getBean(String beanId) { ServletContext ser ...
- 大数据之scala基本语法学习
package edu.snnu.test object list2 { //把字符串转化成一个char类型的list "99 Red Balloons".toList //> ...
- 0622 python 基础05
使用双重for循环,打印 0~100 # -*- coding: utf-8 -*- # D:\python\test.py def printOneToHundred(): for i in ...
- zoj 3171 The Hidden 7's
这道题,我在网上看到两种dp,不过基本原理是一样的,不过感觉还是后面的一种比较巧妙!因为我对动态不是很熟,自能加上一些自己的理解,写上注释. 1) #include <stdio.h> # ...
- PHP上传图片
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- maven打包成第三方jar包且把pom依赖包打入进来
<build> <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId& ...
- Ubuntu下lamp(PHP+Mysql+Apache)搭建+完全卸载卸载方法
安装apache2 sudo apt-get install apache2 安装完成,运行如下命令重启下: sudo /etc/init.d/apache2 restart 在浏览器里输入http: ...