Java intuitive Solution using a Stack!


  • 0
    T
        public boolean isValidSerialization(String preorder) {
            Stack<Character> st = new Stack();
            st.push('#');
            String[] tokens = preorder.split("\\,");
            if(tokens.length==1 && tokens[0].equals("#"))return true;
            for(String s : tokens){
                if(st.isEmpty())return false;
                    st.pop();
                if(!s.equals("#")){
                    st.push('#');
                    st.push('#');
                }
            }
            return st.isEmpty();
            
        }
    

Log in to reply
 

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