Leetcode 894. All Possible Full Binary Trees
递归
# Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None import functools class Solution: @functools.lru_cache() def allPossibleFBT(self, N: int) -> List[TreeNode]: ans=[] if N==1: return [TreeNode(0)] for i in range(0,N): if (i==0 or i%2==1) and ((N-1-i)==0 or (N-1-i)%2==1): print(i,N-1-i) for l in self.allPossibleFBT(i): for r in self.allPossibleFBT(N-1-i): root=TreeNode(0) root.left=l root.right=r ans.append(root) else: continue return ans
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

更多精彩