package leetcode;

/**
 * https://leetcode.com/problems/count-complete-tree-nodes/
 */
public class Problem222 {
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

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

    private static class Count {
        int count;
        boolean done;
    }

    public int countNodes(TreeNode root) {
        int maxLevel = getNumLevel(root, 0);
//        System.out.println("maxlevel=" + maxLevel);
        Count incomplete = new Count();
        countIncomplete(root, maxLevel, 0, incomplete);
//        System.out.println("countIncomplete=" + countIncomplete);
        return (int) (Math.pow(2, maxLevel) - 1) - incomplete.count;
    }

    private void countIncomplete(TreeNode node, int maxLevel, int level, Count count) {
        if (node == null) {
            if (maxLevel > level) {
                count.count++;
                return;
            }
            count.done = true;
            return;
        }
        countIncomplete(node.right, maxLevel, level + 1, count);
        if (count.done) {
            return;
        }
        countIncomplete(node.left, maxLevel, level + 1, count);
        if (count.done) {
            return;
        }
    }

    private int getNumLevel(TreeNode node, int level) {
        if (node == null) {
            return level;
        }
        return getNumLevel(node.left, level + 1);
    }

    public static void main(String[] args) {
        Problem222 prob = new Problem222();

        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.left.left = new TreeNode(3);
        root.left.left.left = new TreeNode(4);
        root.left.left.right = new TreeNode(5);
        root.left.right = new TreeNode(6);
        root.left.right.left = new TreeNode(7);
        root.left.right.right = new TreeNode(8);
        root.right = new TreeNode(9);
        root.right.left = new TreeNode(10);
        root.right.left.left = new TreeNode(11);
        root.right.right = new TreeNode(12);

        System.out.println(prob.countNodes(root));
    }
}

@@ -318,24 +318,43 @@
  int count;%0A
+        int level;%0A
         bool
@@ -425,96 +425,32 @@
     
-int maxLevel = getNumLevel(root, 0);%0A//        System.out.println(%22maxlevel=%22 + maxLevel
+Count count = new Count(
 );%0A 
@@ -460,15 +460,14 @@
     
-C
+c
 ount
- i
+I
 ncom
@@ -471,29 +471,31 @@
 complete
- = new C
+(root, 0, c
 ount
-(
 );%0A     
@@ -501,54 +501,38 @@
     
-countIncomplete(root, maxLevel, 0, incomplete)
+int maxLevel = count.level + 1
 ;%0A//
@@ -584,26 +584,22 @@
  + count
-Incomplete
+.count
 );%0A     
@@ -648,18 +648,13 @@
 ) - 
-incomplete
+count
 .cou
@@ -715,22 +715,8 @@
 ode,
- int maxLevel,
  int
@@ -786,24 +786,24 @@
 if (
-maxLevel %3E level
+count.level == 0
 ) %7B%0A
@@ -845,32 +845,48 @@
             
-return;%0A
+count.level = level;%0A   
             
@@ -877,33 +877,40 @@
 %0A               
-%7D
+ return;
 %0A            cou
@@ -910,45 +910,72 @@
     
-count.done = true;%0A            return
+%7D else if (count.level == level) %7B%0A                count.count++
 ;%0A  
@@ -976,34 +976,32 @@
 ++;%0A        
-%7D%0A
         
 countIncompl
@@ -992,64 +992,29 @@
     
-countIncomplete(node.right, maxLevel, level + 1, count);
+return;%0A            %7D
 %0A   
@@ -1010,36 +1010,36 @@
       %7D%0A        
-if (
+    
 count.done) %7B%0A  
@@ -1024,35 +1024,40 @@
       count.done
-) %7B
+ = true;
 %0A            ret
@@ -1104,22 +1104,13 @@
 ode.
-left, maxLevel
+right
 , le
@@ -1190,66 +1190,57 @@
     
-%7D%0A%0A
     
-private int getNumLevel(TreeNode node, int level) %7B
+countIncomplete(node.left, level + 1, count);
 %0A   
@@ -1244,36 +1244,34 @@
         if (
-node == null
+count.done
 ) %7B%0A        
@@ -1280,22 +1280,16 @@
   return
- level
 ;%0A      
@@ -1296,58 +1296,8 @@
   %7D%0A
-        return getNumLevel(node.left, level + 1);%0A
     

Update problem 222