Simple solution using stack


  • 0
    N
    public class Solution {
    Stack<String> stack = new Stack();
    public boolean isValidSerialization(String preorder) {
        String[] order = preorder.split(",");
        return (check(order, 0) == order.length);
    }
    private int check(String[] order, int cur) {
        if (cur >= order.length) return -1;
        if (!order[cur].equals("#")) {
            stack.push(order[cur]);
            int res = check(order, cur + 1);
            if (res < 0) return res;
            stack.pop();
            return check(order, res);
        } else {
            return cur + 1;
        }
    }
    

    }


  • 0
    Z

    Actually your stack seems doing nothing here. Change your title.


Log in to reply
 

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