How to prove my solution? It passed.


  • 1
    A
    public boolean isValidSerialization(String preorder) {
    	int numOfNulls = 0;
    	int numOfNodes = 0;
    	StringTokenizer st = new StringTokenizer(preorder, ",");
    	while (st.hasMoreTokens()) {
    		if(numOfNulls - numOfNodes == 1)
    			return false;
    		String token = st.nextToken();
    		if ("#".equals(token))
    			numOfNulls++;
    		else
    			numOfNodes++;
    	}
    	return numOfNulls - numOfNodes == 1;
    }

Log in to reply
 

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