满二叉树是一类二叉树,其中每个结点恰好有 0 或 2 个子结点。
返回包含 N 个结点的所有可能满二叉树的列表。 答案的每个元素都是一个可能树的根结点。
答案中每个树的每个结点都必须有 node.val=0。
你可以按任何顺序返回树的最终列表。
示例:
输入:7
输出:[[0,0,0,null,null,0,0,null,null,0,0],[0,0,0,null,null,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,null,null,null,null,0,0],[0,0,0,0,0,null,null,0,0]]
解释:
提示:
1 <= N <= 20
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/all-possible-full-binary-trees
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
recursive
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { public List<TreeNode> allPossibleFBT(int N) { List<TreeNode> list=new ArrayList<TreeNode>(); if(N==1){ TreeNode root=new TreeNode(0); list.add(root); return list; }else{ for(int i=1;i<N;i+=2){ List<TreeNode> lefts=allPossibleFBT(i); List<TreeNode> rights=allPossibleFBT(N-i-1); for(TreeNode l:lefts){ for(TreeNode r:rights){ TreeNode root=new TreeNode(0); root.left=l; root.right=r; list.add(root); } } } return list; } } }
memory search
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { Map<Integer, List<TreeNode>> memo = new HashMap(); public List<TreeNode> allPossibleFBT(int N) { if (!memo.containsKey(N)) { List<TreeNode> ans = new LinkedList(); if (N == 1) { ans.add(new TreeNode(0)); } else if (N % 2 == 1) { for (int x = 0; x < N; ++x) { int y = N - 1 - x; for (TreeNode left: allPossibleFBT(x)) for (TreeNode right: allPossibleFBT(y)) { TreeNode bns = new TreeNode(0); bns.left = left; bns.right = right; ans.add(bns); } } } memo.put(N, ans); } return memo.get(N); } }