#Leet Code# Unique Tree

语言:Python

描述:使用递归实现

 1 class Solution:
 2     # @return an integer
 3     def numTrees(self, n):
 4         if n == 0:
 5             return 0
 6         elif n == 1:
 7             return 1
 8         else:
 9             part_1 = self.numTrees(n-1) * 2
10             part_2 = 0
11
12             for i in range(1,n-1):
13                 part_left = self.numTrees(i)
14                 part_right = self.numTrees(n - 1 - i)
15                 part_2 += part_left * part_right
16
17             return part_1 + part_2

#Leet Code# Unique Tree,布布扣,bubuko.com

时间: 2024-10-10 16:49:43

#Leet Code# Unique Tree的相关文章

#Leet Code# Same Tree

语言:Python 描述:使用递归实现 1 # Definition for a binary tree node 2 # class TreeNode: 3 # def __init__(self, x): 4 # self.val = x 5 # self.left = None 6 # self.right = None 7 8 class Solution: 9 # @param p, a tree node 10 # @param q, a tree node 11 # @return

#Leet Code# Unique Path

描述: 使用了递归,有些计算是重复的,用了额外的空间,Version 1是m*n Bonus:一共走了m+n步,例如 m = 2, n = 3 [#, @, @, #, @],所以抽象成数学问题,解是C(m + n, m) 代码: 1 class Solution: 2 # @return an integer 3 def __init__(self): 4 self.record = {} 5 6 def uniquePaths(self, m, n): 7 if m == 0 or n ==

Leet Code -- Unique BST

对于数字n(大于1),从1到n有多少种binary search tree(BST序列)?当n=3时,BST序列为: 1         3     3    2     1     \         /     /      / \      \     3      2    1    1  3     2     /       /       \                  \   2      1         2                3共5种. 分析: N=1时,

#Leet Code# Convert Sorted Array to Binary Search Tree

描述:递归 代码: 1 class Solution: 2 # @param num, a list of integers 3 # @return a tree node 4 def sortedArrayToBST(self, num): 5 if len(num) == 0: 6 return None 7 8 mid_index = len(num) / 2 9 10 tmp_tree = TreeNode(num[mid_index]) 11 tmp_tree.left = self.

#Leet Code# Populating Next Right Pointers in Each Node II

描述:注意需要先self.connect(right)再self.connect(left),否则会有case通不过,原因是左边递归执行时依赖与右边的next已经建立,而先执行connect(left)的话右边还没有完成关系的建立. 代码: 1 class Solution: 2 # @param root, a tree node 3 # @return nothing 4 def doSth(self, nextNode, conNode): 5 while nextNode is not

#Leet Code# Root to leaf

语言:Python 描述:使用递归实现 1 def getList(self, node): 2 if node is None: 3 return [] 4 5 if node.left is None and node.right is None: 6 return [[node.val]] 7 8 result = [] 9 for item in self.getList(node.left): 10 result.append([node.val] + item) 11 12 for

#Leet Code# Evaluate Reverse Polish Notation

描述:计算逆波兰表达法的结果 Sample: ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9 ["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6 使用stack实现: 1 def is_op

leet code Sort List

*/--> pre.src {background-color: Black; color: White;} pre.src {background-color: Black; color: White;} leet code Sort List 对链表使用快慢指针归并排序 Sort List Sort a linked list in O(n log n) time using constant space complexity. /** * Definition for singly-lin

#Leet Code# Sqrt

描述:log(n) 代码: 1 class Solution: 2 # @param x, an integer 3 # @return an integer 4 def getVal(self, begin, end, x): 5 if end == begin : 6 return begin 7 if end == begin + 1: 8 return begin 9 10 while True: 11 mid = (begin + end) / 2 12 tmp = mid * mid