637. Average of Levels in Binary Tree

637. Average of Levels in Binary Tree

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public List averageOfLevels(TreeNode root) {
       List out = new ArrayList();
        Queue q = new LinkedList();
        q.add(root);
        
        while(! q.isEmpty()){
            int len = q.size();
            Double sum = 0.0;
            for (int i = 0; i < len; i++){
               TreeNode c = q.remove();
                if (c != null){
               sum += c.val; 
                if(c.left !=null)q.add(c.left);
                if(c.right != null)q.add(c.right);
                }

            }
                          out.add(sum/len);

        }
      return out;  
    }
}

868. Binary Gap

868. Binary Gap

class Solution {
    public int binaryGap(int N) {
        String binary = Integer.toBinaryString(N);
        int first = 0;
        while (first < binary.length() && binary.charAt(first) != '1'){
            first ++;}
        if (first == binary.length()) return 0;
        int diff = Integer.MIN_VALUE;
        int current = first;
        for (int i = first+1; i  diff){
                    diff = i - current;
                }
                current = i;
            }
        }
        if (diff == Integer.MIN_VALUE) return 0;
        return diff;
    }
}

884. Uncommon Words from Two Sentences

884. Uncommon Words from Two Sentences

class Solution {
    public String[] uncommonFromSentences(String A, String B) {
                List outputList = new ArrayList();  
String[] As = A.split(" ");
String[] Bs = B.split(" ");
        Map s = new HashMap();
        for (int i = 0; i < As.length; i++){
            if(s.get(As[i]) == null) s.put(As[i],1);
            else s.put(As[i],s.get(As[i])+1);
        }
        for (int i = 0; i < Bs.length; i++){
            if(s.get(Bs[i]) == null) s.put(Bs[i],1);
            else s.put(Bs[i],s.get(Bs[i])+1);
        }
        
        for (String a: s.keySet()){
            if ( s.get (a) == 1) outputList.add(a);
        }
        
        return outputList.toArray(new String[outputList.size()]);
    }
}

938. Range Sum of BST

938. Range Sum of BST

 

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public int rangeSumBST(TreeNode root, int L, int R) {
        int out = 0;
        out = rangeSumBSTHelper(root,L,R);
        return out;
    }
    public static int rangeSumBSTHelper(TreeNode root, int L, int R){
        if(root == null) return 0;
        int total = 0;
        if( root.val >= L && root.val <= R)
          total+= root.val;
        
        if (root.right != null)
         total+= rangeSumBSTHelper(root.right,L,R);
         if (root.left != null)   
        total+=rangeSumBSTHelper(root.left,L,R);
        
        return total;
    }
}

872. Leaf-Similar Trees

872. Leaf-Similar Trees

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public boolean leafSimilar(TreeNode root1, TreeNode root2) {
      ArrayList tree1 = new ArrayList();
      ArrayList tree2 = new ArrayList(); 
        traverse(root1, tree1);
        traverse(root2, tree2);
        
        if (tree1.size() != tree2.size()) return false;
        for (int i = 0; i < tree1.size(); i++){
            if (tree1.get(i) != tree2.get(i)) return false;
        }
        return true;
    }
public static void traverse (TreeNode root, ArrayList tree){
    
    if( root.right == null && root.left == null){
        tree.add(root.val);
        return;
    }
    if (root.right != null) traverse (root.right, tree);
    if (root.left != null) traverse (root.left, tree);
}    
    
}