Why my answer is not correct?


  • 0
    Q
    public class Solution {
        public boolean isSameTree(TreeNode p, TreeNode q) {
                Queue<TreeNode> a=new LinkedList<TreeNode>();
                Queue<TreeNode> b=new LinkedList<TreeNode>();
                if(p!=null);a.offer(p);
                if(q!=null);b.offer(q);
                while(!a.isEmpty()&&!b.isEmpty()){
                    TreeNode newa=a.poll();
                    TreeNode newb=b.poll();
                    if(newa.val!=newb.val)return false;
                    if(newa.right!=null)a.offer(newa.right);
                    if(newb.right!=null)b.offer(newa.right);
                    if(a.size()!=b.size())return false;
                     if(newa.left!=null)a.offer(newa.left);
                    if(newb.left!=null)b.offer(newa.left);
                    if(a.size()!=b.size())return false;
                    }
                    return a.size()==b.size();
                }
                
    }

  • 0

    That doesn't get "wrong answer" but "runtime error".

    Too many semi-colons and typical copy-paste-forgotToChangeTheCopy errors.


  • 0
    A

    For starters why are you checking newb right node then adding newa right node..

    if(newb.right!=null)b.offer(newa.right);
    

    and similarly for left nodes.


Log in to reply
 

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