During debuging the Symmetric Tree,if you exectue it will be wrong what is the problem


  • 0
    K

    public boolean isSymmetric(TreeNode root) {
    LinkedList<TreeNode>list=new LinkedList<TreeNode>();
    int[]a=new int[Integer.MAX_VALUE];
    if(root==null)
    return true;
    list.add(root);
    a[0]=root.val;
    int start=0;
    int end=0;
    TreeNode result=null;

    	 while(start<list.size()){
    		 end=list.size();
    		 
    		 
    		 while(start<end){
    			 result=list.get(start);
    	
    		
    		 if(result.left!=null){
    			 list.add(result.left);	
    			 a[2*start+1]=result.left.val;
    		 }
    		 
    		 if(result.right!=null){
    			 list.add(result.right);
    			 a[2*start+2]=result.right.val;
    		 }	 
    		 start++;
    		 }
    		 
    	 }
    	 for(int i=0;i<=2*list.size();i++)
    		 System.out.print(a[i]+"\t");
    	 System.out.println();
    	 int level=0;
    	 int n=2*list.size();
    	 while(n!=0){
    		 level++;
    		 n=n/2;
    	 }
      System.out.println(level);
      for(int i=1;i<=level;i++){
    	for(int j=(int)Math.pow(2, i-1)-1,k=(int)Math.pow(2, i)-2;j<=k;j++,k--){
    		if(a[j]!=a[k])
    			return false;
    	}
    }	
    	 
    return true;
      }

Log in to reply
 

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