1022. Sum of Root To Leaf Binary Numbers

1022. Sum of Root To Leaf Binary Numbers

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    int sum = 0;
    public void construct(TreeNode root,String prev){
        if (root == null) return;
        if( root.left == null && root.right == null){
            sum+=Integer.parseInt(prev+root.val,2);
            return;
        }
       construct(root.left,prev+""+root.val);
       construct(root.right,prev+""+root.val);    
    }
    public int sumRootToLeaf(TreeNode root) {
        if (root == null) return 0;
        if(root.left == null && root.right == null) return root.val;
         construct(root.left,root.val+"");
         construct(root.right,root.val+"");
        return sum;
    }
}

107. Binary Tree Level Order Traversal II

107. Binary Tree Level Order Traversal II

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public List<List> levelOrderBottom(TreeNode root) {
        if (root == null) return new LinkedList();
        List<List> out = new ArrayList();
Queue   q = new LinkedList(); 
        q.offer(root);
        while(!q.isEmpty()){
            ArrayList current = new ArrayList();
            int size = q.size();//we must put it in variable is the size changes each loop
            for(int i =0; i < size; i++){
            TreeNode c = q.poll();
            if(c != null){
                current.add(c.val);
            if(c.left != null)
                q.offer(c.left);
            if(c.right != null)
                q.offer(c.right);
            }
            
        }
            out.add(current);
        }
         Collections.reverse(out);
        return out;
    }
    
}

101. Symmetric Tree

101. Symmetric Tree

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public static boolean isMirror(TreeNode r, TreeNode l){
        if( r == null && l == null) return true;
        if( r == null && l != null) return false;
        if( r != null && l == null) return false;
        if (r.val != l.val) return false;
        if (r.val == l.val) 
        return isMirror(l.right,r.left) && isMirror(l.left, r.right);
         else return false;
    }
    public boolean isSymmetric(TreeNode root) {
        if (root == null) return true;
        
        return isMirror(root.right,root.left);
    }
}

21. Merge Two Sorted Lists

21. Merge Two Sorted Lists

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
 public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
     if (l1 == null && l2 == null) return null;
        if (l1 == null) return l2;
    	if (l2 == null) return l1;
        ListNode out = null;
        ListNode current = null;
        if(l1.val == l2.val) {
        	out =new ListNode(l1.val);
        	l1 = l1.next;
        	out.next = new ListNode(l2.val);
        	l2 = l2.next;
        	current = out.next;
        }else
        if(l1.val < l2.val) {
        	out =new ListNode(l1.val);
        	l1 = l1.next;
        	current = out;
        }
        else {
        	out =new ListNode(l2.val);
        	l2 = l2.next;
        	current = out;
        }
        while(l1 != null || l2 != null) {
        	if(l1 == null && l2 == null)
        		return out;
        	else if (l1 == null && l2 != null) {
        		current.next = l2;
        	return out;	
        	}
        	else if ( l1 != null && l2 == null) {
        		current.next = l1;
        	return out;	
        	}
        	else if (l1.val  l2.val) {
        		ListNode newListNode = new ListNode(l2.val);
        		current.next = newListNode;
        		current = current.next;
        		l2 = l2.next;
        	}
        }
        return out;
    }

}

1046. Last Stone Weight

1046. Last Stone Weight

class Solution {
      public int lastStoneWeight(int[] stones) {
    	PriorityQueue queue = new PriorityQueue(new Comparator() {                                                                                      @Override 
    		public int compare(Integer o1, Integer o2) {       
    		return - Integer.compare(o1,o2); } });
    
    	for (int i = 0; i = 2) {
    		int x = queue.poll();
    		int y = queue.poll();
    		if (x == y) continue;
    		if ( x <= y && x != y) queue.offer(y-x);
    		else if( y 0 ?queue.poll():0;
}}

1009. Complement of Base 10 Integer

https://leetcode.com/problems/complement-of-base-10-integer/

class Solution {
    public int bitwiseComplement(int N) {
    String x = Integer.toBinaryString(N);
        StringBuilder out = new StringBuilder();
        for (int i = 0; i <span id="mce_SELREST_start" style="overflow:hidden;line-height:0;">&#65279;</span>&lt; x.length(); i++){
            if(x.charAt(i) == &#039;0&#039;)
                out.append(&quot;1&quot;);
            else out.append(&quot;0&quot;);
        }
        return Integer.parseInt(out.toString(), 2);
}}

Better Solution