2019年3月20日 894. All Possible Full Binary Trees
实际上感觉是带参的宽搜,但是因为懒得写。。还是强行写了深搜。
这道题在lt上提交有bug,好像是因为我用了node的未声明变量?单独测试是没问题的
class Solution(object): result = [] def buildNode(self, v, father, root): ret = TreeNode(v) ret.father = father ret.root = root return ret def possibleFBT(self, n, node): if n == 0: self.result.append(copy.deepcopy(node.root)) return node.left = self.buildNode(0, node, node.root) node.right = self.buildNode(0, node, node.root) self.possibleFBT(n-2, node.left) if n-2 > 0: self.possibleFBT(n-2, node.right) if node.father and n-2 > 0 and node.father.right != node: self.possibleFBT(n-2, node.father.right) node.left = None node.right = None def allPossibleFBT(self, N): """ :type N: int :rtype: List[TreeNode] """ if N % 2 == 0: return [] root = self.buildNode(0, None, None) root.root = root self.possibleFBT(N-1, root) return self.result