码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode 894. All Possible Full Binary Trees

时间:2019-04-23 09:29:23      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:oss   return   pre   UNC   col   []   continue   efi   def   

递归

# 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

标签:oss   return   pre   UNC   col   []   continue   efi   def   

原文地址:https://www.cnblogs.com/zywscq/p/10754188.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!