Simple JS solution


  • 0
    A
    /**
     * @param {string} preorder
     * @return {boolean}
     */
    var isValidSerialization = function(preorder) {
        var nodes = preorder.split(',');
        var i = 0, leaf = 1;
        for(i = 0; i < nodes.length && leaf > 0; i++){
            var node = nodes[i];
            if(node !== '#'){
                leaf++;
            }else{
                leaf--;
            }
        }
        return leaf === 0 && i == nodes.length;
    };

Log in to reply
 

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