Level Order Traversal Approach using two queue


#1

public class Solution
{
public int solve(TreeNode root)
{
//rpa
Queue mq=new LinkedList();
Queue cq=new LinkedList();

    if(root==null)
    {
        return 0;
    }
    
    mq.add(root);
    
    int max=Integer.MIN_VALUE;
    int sum=0;
    while(!mq.isEmpty())
    {
        //rpa
        
        TreeNode temp=mq.remove();
        
        sum=sum+temp.val;
        
        if(temp.left!=null)
        {
            cq.add(temp.left);
        }
        if(temp.right!=null)
        {
            cq.add(temp.right);
        }
        
        if(mq.isEmpty())
        {
            mq=cq;
            cq=new LinkedList<TreeNode>();
            
            if(sum>max)
            {
                max=sum;
            }
            sum=0;
            
        }
        
        
    }
    
    return max;
}

}