package com.fishercoder.solutions;

import com.fishercoder.common.classes.TreeNode;

/**Given a binary tree, count the number of uni-value subtrees.

 A Uni-value subtree means all nodes of the subtree have the same value.

 For example:
 Given binary tree,
     5
    / \
   1   5
  / \   \
 5  5   5
 return 4.

 */
public class _250 {

    public int countUnivalSubtrees(TreeNode root) {
        int[] count = new int[1];
        helper(root, count);
        return count[0];
    }

    private boolean helper(TreeNode node, int[] count) {
        if (node == null) {
            return true;
        }
        boolean left = helper(node.left, count);
        boolean right = helper(node.right, count);
        if (left && right) {
            if (node.left != null && node.val != node.left.val) {
                return false;
            }
            if (node.right != null && node.val != node.right.val) {
                return false;
            }
            count[0]++;
            return true;
        }
        return false;
    }

}

@@ -82,262 +82,69 @@
 e;%0A%0A
-/**Given a binary tree, count the number of uni-value subtrees.%0A%0A A Uni-value subtree means all nodes of the subtree have the same value.%0A%0A For example:%0A Given binary tree,%0A     5%0A    / %5C%0A   1   5%0A  / %5C   %5C%0A 5  5   5%0A return 4.%0A%0A */%0Apublic class _250 %7B%0A%0A
+public class _250 %7B%0A%0A    public static class Solution1 %7B%0A    
     
@@ -199,16 +199,20 @@
         
+    
 int%5B%5D co
@@ -229,16 +229,20 @@
 int%5B1%5D;%0A
+    
         
@@ -266,24 +266,28 @@
 t);%0A        
+    
 return count
@@ -299,15 +299,23 @@
     
-%7D%0A%0A
+    %7D%0A%0A   
     
+ 
 priv
@@ -363,32 +363,36 @@
 ount) %7B%0A        
+    
 if (node == null
@@ -399,32 +399,36 @@
 ) %7B%0A            
+    
 return true;%0A   
@@ -424,34 +424,42 @@
 n true;%0A        
+  
-%7D%0A
+  %7D%0A    
         boolean 
@@ -491,32 +491,36 @@
 count);%0A        
+    
 boolean right = 
@@ -550,24 +550,28 @@
 t);%0A        
+ 
+   
 if (left && 
@@ -575,24 +575,28 @@
 && right) %7B%0A
+    
             
@@ -657,32 +657,36 @@
                 
+   
+ 
 return false;%0A  
@@ -687,34 +687,42 @@
 se;%0A            
-%7D%0A
+    %7D%0A    
             if (
@@ -781,32 +781,36 @@
                 
+    
 return false;%0A  
@@ -811,34 +811,42 @@
 se;%0A            
-%7D%0A
+    %7D%0A    
             coun
@@ -857,32 +857,36 @@
 ++;%0A            
+    
 return true;%0A   
@@ -890,26 +890,33 @@
         
-%7D%0A
+    %7D%0A   
         
 return f
@@ -899,32 +899,33 @@
    %7D%0A           
+ 
 return false;%0A  
@@ -926,13 +926,22 @@
 se;%0A    
+    %7D%0A    
 %7D
-%0A
 %0A%7D%0A

refactor 250