Advertisement
ogv

Untitled

ogv
Nov 18th, 2019
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.91 KB | None | 0 0
  1. // Runtime: 5 ms, faster than 35.54% of Java online submissions for All Possible Full Binary Trees.
  2. // Memory Usage: 48.8 MB, less than 30.77% of Java online submissions for All Possible Full Binary Trees.
  3. class Solution {
  4.     public List<TreeNode> allPossibleFBT(int N) {
  5.         List<TreeNode> results = new ArrayList<>();
  6.        
  7.         if (N % 2 == 0) return results;
  8.        
  9.         if (N == 1) {
  10.             results.add(new TreeNode(0));
  11.             return results;
  12.         }
  13.        
  14.         for (int i = 1; i <= N - 2; i += 2) {
  15.             for (TreeNode left: allPossibleFBT(i))
  16.                 for (TreeNode right: allPossibleFBT(N - 1 - i)) {
  17.                     TreeNode r = new TreeNode(0);
  18.                     r.left = left;
  19.                     r.right = right;
  20.                     results.add(r);                    
  21.                 }
  22.         }
  23.        
  24.         return results;
  25.     }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement