What is wrong with my code???


  • 0
    Y
    public class Solution {
    public int maxDepth(TreeNode root) {
        int h=0;  
    	Boolean q1=false,q2=false;   
    	TreeNode temp;
    	PriorityQueue <TreeNode> queue1=new PriorityQueue<TreeNode>();
    	PriorityQueue <TreeNode> queue2=new PriorityQueue<TreeNode>();
    	if(root==null)
    		return h;
    	queue1.offer(root);
    	h=h+1;
    	q1=true;
    	if(root.left!=null)
    	{
    		q2=true;
    		queue2.offer(root.left);
    	}
    	if(root.right!=null)
    	{
    		q2=true;
    		queue2.offer(root.right);
    	}
    	queue1.remove();
    	q1=false;
    	while(q1^q2) 
    	{
    		if(q1)
    		{
    			while(!queue1.isEmpty())
    			{				
    				temp=queue1.poll();
    				if(temp.left!=null)
    				{
    					if(!q2)
    						q2=true;
    					queue2.offer(temp.left);
    				}
    				if(temp.right!=null)
    				{
    					if(!q2)
    						q2=true;
    					queue2.offer(temp.right);
    				}
    			}
    			q1=false;
    		}else
    		{
    			while(!queue2.isEmpty())
    			{
    				temp=queue2.poll();
    				if(temp.left!=null)
    				{
    					queue1.offer(temp.left);
    					if(!q1)
    						q1=true;
    				}
    				if(temp.right!=null)
    				{
    					if(!q1)
    						q1=true;
    					queue1.offer(temp.right);
    				}
    			}
    			q2=false;
    		}
    		h=h+1;
    	}
        return h;
    }
    

    }

    //this is my solution,but it said "Line 30: java.lang.ClassCastException: TreeNode cannot be cast to java.lang.Comparable Last executed"。who can tell me what's wrong with my code?


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.