[JAVA] Simple and Clean with easy explanation
\n\t  9\n    /   \\\n   3     2\n  / \\   / \\\n 4   1  #  6\n/ \\ / \\   / \\\n# # # #   # #\n\nExplanation\nIn a binary tree, if we consider null as leaves, t
```\n\t  9\n    /   \\\n   3     2\n  / \\   / \\\n 4   1  #  6\n/ \\ / \\   / \\\n# # # #   # #\n```\n**Explanation**\nIn a binary tree, if we consider null as leaves, then\n1. all non-null node provides 2 outdegree and 1 indegree (2 children and 1 parent), except root\n2.   all null node provides 0 outdegree and 1 indegree (0 child and 1 parent).\n\nSuppose we try to build this tree. During building, we record the difference between out degree and in degree diff = outdegree - indegree. When the next node comes, we then decrease diff by 1, because the node provides an in degree. If the node is not null, we increase diff by 2, because it provides two out degrees. If a serialization is correct, diff should never be negative and diff will be zero when finished.\n```\npublic boolean isValidSerialization(String preorder) {\n    String[] nodes = preorder.split(",");\n    int diff = 1;\n    for (String node: nodes) {\n        if (--diff < 0) return false;\n        if (!node.equals("#")) diff += 2;\n    }\n    return diff == 0;\n}\n```\n\nPlease Upvote so it remain at top
['String', 'Binary Tree', 'Java']