package leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 * https://leetcode.com/problems/all-possible-full-binary-trees/
 */
public class Problem894 {
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

    public List<TreeNode> allPossibleFBT(int N) {
        List<TreeNode> result = new ArrayList<>();
        if (N <= 2 || N % 2 == 0) {
            return result;
        }
        allPossibleFBT(N, 1, new TreeNode(0));
        return result;
    }

    private static void allPossibleFBT(int n, int count, TreeNode node) {
        node.left = new TreeNode(0);
        node.right = new TreeNode(0);
        if (count + 2 == n) {
            System.out.println("here");
        } else {
            allPossibleFBT(n, count + 2, node.left);
            allPossibleFBT(n, count + 2, node.right);
        }
    }

    public static void main(String[] args) {
        Problem894 prob = new Problem894();
//        System.out.println(prob.allPossibleFBT(5).size()); // 2
        System.out.println(prob.allPossibleFBT(7).size()); // 5
//        System.out.println(prob.allPossibleFBT(6).size()); // 0
    }
}

@@ -446,23 +446,83 @@
  (N 
-%3C= 2 %7C%7C 
+== 1) %7B%0A            result.add(new TreeNode(0));%0A        %7D else if (
 N %25 2 
-=
+!
 = 0)
@@ -532,38 +532,53 @@
             
-return result;
+for (int i = 0; i %3C N; i++) %7B
 %0A        %7D%0A 
@@ -578,18 +578,16 @@
     
-%7D%0A
         
 allP
@@ -586,294 +586,243 @@
     
-allPossibleFBT(N, 1, new TreeNode(0));%0A        return result;%0A    %7D%0A%0A    private static void allPossibleFBT(int n, int count, TreeNode node) %7B%0A        node.left = new TreeNode(0);%0A        node.right = new TreeNode(0);%0A        if (count + 2 == n) %7B%0A            System.out.println(%22here%22)
+int j = N - 1 - i;%0A                for (TreeNode left : allPossibleFBT(i))%0A                    for (TreeNode right : allPossibleFBT(j)) %7B%0A                        TreeNode node = new TreeNode(0);%0A                        node.left = left
 ;%0A  
@@ -831,17 +831,10 @@
     
-%7D else %7B%0A
+  
     
@@ -845,100 +845,69 @@
     
-allPossibleFBT(n, count + 2, node.left);%0A            allPossibleFBT(n, count + 2, node.right
+  node.right = right;%0A                        result.add(node
 );%0A 
@@ -917,301 +917,68 @@
     
-%7D%0A
     
-%7D%0A%0A
     
-public static void main(String%5B%5D args) %7B%0A        Problem894 prob = new Problem894();%0A//        System.out.println(prob.allPossibleFBT(5).size()); // 2%0A        System.out.println(prob.allPossibleFBT(7).size()); // 5%0A//        System.out.println(prob.allPossibleFBT(6).size()); // 0
+    %7D%0A            %7D%0A        %7D%0A        return result;
 %0A   

Solve problem 894