why have NullPointerException


  • 0
    J

    Line 19: if(temp.left.val < temp.val && temp.val < temp.right.val)
    have NullPointerException
    and how to fix it?
    thanks!

    public class Solution {
        public boolean isValidBST(TreeNode root) {
            if(root == null) return true;
            Queue<TreeNode> que = new LinkedList<TreeNode>();
            que.offer(root);
            while(!que.isEmpty()){
                int size = que.size();
                for(int i = 0; i < size; i++){
                    TreeNode temp = que.poll();
                    if(temp.left.val < temp.val && temp.val < temp.right.val){
                        que.offer(temp.left);
                        que.offer(temp.right);
                    }else return false;
                }
    
            }
            return true;
        }
    }
    

Log in to reply
 

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