Leetcode 894. All Possible Full Binary Trees

Posted 周洋的Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

 

以上是关于Leetcode 894. All Possible Full Binary Trees的主要内容,如果未能解决你的问题,请参考以下文章

leetcode_894. All Possible Full Binary Trees

leetcode 894. 所有可能的满二叉树(All Possible Full Binary Trees)

894. 所有可能的真二叉树

894. 所有可能的真二叉树

894. All Possible Full Binary Trees

894. All Possible Full Binary Trees