My simple solution using stack


  • 0
    G
    class Solution(object):
        def isValidSerialization(self, preorder):
            s = preorder.split(',');
            i = len(s) - 1;
            while i >= 0:
                if s[i] != '#':
                    if not s or s.pop() != '#':
                        return(False);
                    if not s or s.pop() != '#':
                        return(False);
                    s[i] = '#';
                i -= 1;
            return(len(s) == 1 and s[0] == '#');

  • 0
    L
    This post is deleted!

Log in to reply
 

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