Java beats 98%


  • 0
    S

    ···
    int len = preorder.length();
    if(len == 0) return true;
    int[] stack = new int[len / 4 + 1]; // the number of child nodes are not found(2,1,0)
    int head = 0, l = 0;
    if(preorder.charAt(0) != '#') stack[l++] = 2;
    while(true){
    while(head < len && preorder.charAt(head) != ','){
    head++;
    }
    head++;
    if(head >= len) break;
    if(l == 0) return false;
    if(--stack[l - 1] == 0) l--; // the number of child nodes are not found is 0, out of the stack;
    if(preorder.charAt(head) != '#') stack[l++] = 2;
    }
    if(l != 0) return false;
    return true;
    ···


Log in to reply
 

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