Leetcode-Tree

作者: 浩泽Hauser | 来源:发表于2019-08-12 07:25 被阅读0次

    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;
        }
    }
    

    相关文章

      网友评论

        本文标题:Leetcode-Tree

        本文链接:https://www.haomeiwen.com/subject/zptyjctx.html