Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Given a string of comma-separated values preorder,
- // return true if it is a correct preorder traversal serialization of a binary tree.
- /* Explanation
- In a binary tree, if we consider null as leaves, then
- All non-null node provides 2 outdegree and 1 indegree (2 children and 1 parent), except root
- All null node provides 0 outdegree and 1 indegree (0 child and 1 parent).
- Suppose we try to build this tree. During building, we record 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.
- */
- public boolean isValidSerialization(String preorder) {
- String[] nodes = preorder.split(",");
- int diff = 1;
- for (String node: nodes) {
- if (--diff < 0) return false;
- if (!node.equals("#")) diff += 2;
- }
- return diff == 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement