Grokking the Coding Interview: Patterns for Coding Questions
Vote

0% completed

​

Problem Challenge 2: Structurally Unique Binary Search Trees (hard)

.....

.....

.....

Like the course? Get enrolled and start learning!
A

Anonymous

· 3 years ago

It's not required to reconstruct cached data as we only use them either as left or right child without breaking any relationships between parent-to-child nodes.

Should not it be "better" than the provided solution?

#class TreeNode: #  def __init__(self, val): #    self.val = val #    self.left = None #    self.right = None class Solution:   def findUniqueTrees(self, n):     if n == 0:       return []     cache = {}     def dp(start, end):       if start > end:         return [None]       pair = (start, end)       if pair not in cache:         res = []         for i in range(start, end + 1):           left = dp(start, i - 1)           right = dp(i + 1, end)           for l in left:             for r in right:               root = TreeNode(i)               root.left

Reading Progress

0%


Vote for new content