-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuniqueBinarySearchTreesII.py
More file actions
33 lines (30 loc) · 1008 Bytes
/
uniqueBinarySearchTreesII.py
File metadata and controls
33 lines (30 loc) · 1008 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Source: https://leetcode.com/problems/unique-binary-search-trees-ii/
# Author: Miao Zhang
# Date: 2021-01-15
# 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]:
if n == 0:
return []
return self.dfs(1, n)
def dfs(self, left, right):
if left > right:
return [None]
res = []
for i in range(left, right + 1):
left_nodes = self.dfs(left, i - 1)
right_nodes = self.dfs(i + 1, right)
for left_node in left_nodes:
for right_node in right_nodes:
root = TreeNode(i)
res.append(root)
root.left = left_node
root.right = right_node
return res