Leetcode 894. All Possible Full Binary Trees.
class Solution {
public List<TreeNode> allPossibleFBT(int N) {
List<TreeNode> list = new ArrayList<>();
if(N%2==0) return list;
if(N==1){
list.add(new TreeNode(0));
return list;
}
for(int i=1; i<N; i+=2){
List<TreeNode> lList = allPossibleFBT(i);
List<TreeNode> rList = allPossibleFBT(N-i-1);
for(TreeNode left : lList){
for(TreeNode right : rList){
TreeNode cur = new TreeNode(0);
cur.left = left;
cur.right = right;
list.add(cur);
}
}
}
return list;
}
}
网友评论