128ms --- using javascript


  • 0
    J
    /**
     * @param {string} preorder
     * @return {boolean}
     */
    var isValidSerialization = function(preorder) {
        
            var arr = preorder.split(',');
            if (arr.length == 1 && arr[0] == '#') {
                return true;
            }
            if (arr.length < 3) {
                return false;
            }
        function process(arr) {
            if (arr[0] == '#') {
                return;
            }
            for (var i = 1; i < arr.length - 1; i++) {
                if (arr[i] == '#' &&  arr[i + 1] == '#') {
                    arr.splice(i - 1, 2);
                    process(arr);
                    break;
                }
            }
        }
        process(arr);
        return arr.length == 1;
    };

Log in to reply
 

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