Leetcode-Tree

Leetcode 894. All Possible Full Binary Trees.

class Solution {
    public List allPossibleFBT(int N) {
        List list = new ArrayList<>();
        if(N%2==0) return list;
        if(N==1){
            list.add(new TreeNode(0));
            return list;
        }
        for(int i=1; i lList = allPossibleFBT(i);
            List 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;
    }
}

你可能感兴趣的:(Leetcode-Tree)