Could you please point my faults?I'm confused now.


  • 0
    L

    public List<Integer> preorderTraversal(TreeNode root) {
    ArrayList<Integer> list=new ArrayList<Integer>();
    if(root==null){list.add(0);return list;}
    readnode(root,list);
    return list;
    }

    public static void readnode(TreeNode p,ArrayList<Integer> list){
        
         if(p.left!=null){
            readnode(p.left,list);
            list.add(p.val);
            readnode(p.right,list);
        }
        if(p.left==null&&p!=null){
            list.add(p.val);
            readnode(p.right,list);
        }
       
    }
    

    }

    Runtime Error Message: Line 20: java.lang.NullPointerException
    Last executed input: {1}


Log in to reply
 

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