1.code
/**
* 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>> map = new HashMap<>();
public List<TreeNode> allPossibleFBT(int N) {
if(!map.containsKey(N)){
List<TreeNode> list = new ArrayList();
if(N==1)list.add(new TreeNode(0));
else if(N%2==1){
for(int i=0;i<N;i++){
int j=N-i-1;
for(TreeNode x:allPossibleFBT(i)){
for(TreeNode y:allPossibleFBT(j)){
TreeNode ans = new TreeNode(0);
ans.left=x;
ans.right = y;
list.add(ans);
}
}
}
}
map.put(N,list);
}
return map.get(N);
}
}
2.题解分析
*满足结果的满二叉树一定只有奇数个节点
*从根节点开始,如果一共N个节点,左子树有i个节点,右子树只有N-i-1个节点
这样从根节点开始遍历递归,运用map进行记忆,这样便能解决此题。