标签:mamicode int efi com src for nbsp bin 图片
递归
思路:
代码:
# Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def generateTrees(self, n: int) -> List[TreeNode]: def generate_trees(start, end): if start > end: return [None,] all_trees = [] for i in range(start, end + 1): left_trees = generate_trees(start, i - 1) right_trees = generate_trees(i + 1, end) for l in left_trees: for r in right_trees: current_tree = TreeNode(i) current_tree.left = l current_tree.right = r all_trees.append(current_tree) return all_trees return generate_trees(1, n) if n else []
标签:mamicode int efi com src for nbsp bin 图片
原文地址:https://www.cnblogs.com/nilhxzcode/p/13172505.html