Not so elegant but straight forward Java solution


  • 0
    L

    ...
    public class Solution {
    int index = 0;
    public boolean isValidSerialization(String preorder) {
    String[] values = preorder.split(",");
    int total = values.length;

    	    	return isValid(values, total) && index == total;
    	    }
    	    
    	    boolean isValid(String[] values, int total) {
    	    	if (index >= total) return false;
    	    	if (values[index].equals("#")) {
    	    		index++;
    	    		return true;
    	    	}
    	    	index++;
    	    	return isValid(values, total) && isValid(values, total);
    	    	
    	    }
    	}
    

    ...


Log in to reply
 

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