package leetcode;

/**
 * https://leetcode.com/problems/verify-preorder-serialization-of-a-binary-tree/
 */
public class Problem331 {
    public boolean isValidSerialization(String preorder) {
        String[] nodes = preorder.split(",");
        for (int i = 0; i < nodes.length; i++) {
            // TODO
        }
        return true;
    }

    public static void main(String[] args) {
        Problem331 prob = new Problem331();
        System.out.println(prob.isValidSerialization("9,3,4,#,#,1,#,#,2,#,6,#,#"));
        System.out.println(prob.isValidSerialization("1,#"));
        System.out.println(prob.isValidSerialization("9,#,#,1"));
    }
}

@@ -16,12 +16,38 @@
 e;%0A%0A
+import java.util.Stack;%0A%0A
 /**%0A
+w
  * h
@@ -270,407 +270,1066 @@
     
-for (int i = 0; i %3C nodes.length; i++) %7B%0A            // TODO%0A        %7D%0A        return true;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem331 prob = new Problem331();%0A        System.out.println(prob.isValidSerialization(%229,3,4,#,#,1,#,#,2,#,6,#,#%22));%0A        System.out.println(prob.isValidSerialization(%221,#%22));%0A        System.out.println(prob.isValidSerialization(%229,#,#,1%22));
+Stack%3CNode%3E stack = new Stack%3C%3E();%0A        Direction direction = Direction.LEFT;%0A        for (int i = 0; i %3C nodes.length; i++) %7B%0A            if (nodes%5Bi%5D.equals(%22#%22)) %7B%0A                if (direction == Direction.LEFT) %7B%0A                    direction = Direction.RIGHT;%0A                %7D else %7B%0A                    direction = Direction.LEFT;%0A                %7D%0A                if (stack.size() == 0 && i %3C nodes.length - 1) %7B%0A                    return false;%0A                %7D%0A                if (i %3C nodes.length - 1) %7B%0A                    stack.pop().direction = direction;%0A                %7D%0A                continue;%0A            %7D%0A            Node node = new Node(nodes%5Bi%5D, direction);%0A            stack.add(node);%0A        %7D%0A        return stack.isEmpty();%0A    %7D%0A%0A    private enum Direction %7B%0A        LEFT, RIGHT%0A    %7D%0A%0A    private static class Node %7B%0A        private String node;%0A        private Direction direction;%0A%0A        public Node(String node, Direction direction) %7B%0A            this.node = node;%0A            this.direction = direction;%0A        %7D
 %0A   

Solve problem 331