Java 9ms Simple Recursion Solution


  • 1
    S

    public class Solution {

    public boolean isValidSerialization(String preorder) {
        if (preorder == null || preorder.length() == 0){
            return true;
        }
        
        String[] strs = preorder.split(",");
        int[] index = new int[1];
        
        return helper(strs, index) && index[0] == strs.length;
    }
    
    private boolean helper(String[] strs, int[] index){
        if (!strs[index[0]++].equals("#")){
            //left
            if (index[0] >= strs.length || !helper(strs, index)){
                return false;
            }
            //right
            if (index[0] >= strs.length || !helper(strs, index)){
                return false;
            }
        }
        return true;
    }
    

    }


Log in to reply
 

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