Skip to content

Commit d6e18b5

Browse files
committed
added 897
1 parent 77d4982 commit d6e18b5

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def allPossibleFBT(self, n: int) -> List[Optional[TreeNode]]:
9+
dp = { 0 : [], 1 : [ TreeNode() ] }
10+
11+
def backtrack(n):
12+
if n in dp:
13+
return dp[n]
14+
15+
res = []
16+
for l in range(n):
17+
r = n - 1 - l
18+
leftTrees, rightTrees = backtrack(l), backtrack(r)
19+
20+
for t1 in leftTrees:
21+
for t2 in rightTrees:
22+
res.append(TreeNode(0, t1, t2))
23+
dp[n] = res
24+
return res
25+
26+
return backtrack(n)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy