完美编程

Leetcode-Tree

2019-08-12  本文已影响0人  浩泽Hauser

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;
    }
}
上一篇下一篇

猜你喜欢

热点阅读