Thursday, October 6, 2016

Verify Preorder Sequence in Binary Search Tree


Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary search tree.
You may assume each number in the sequence is unique.
Follow up: Could you do it using only constant space complexity?

It took me some time to understand the solution. The idea is for BST, traversing towards left side always leads a smaller value, and then going right side always lead to a larger value. And the minimum value of all nodes along the right path is the root, so if a node violates this rule, it's not a valid preorder traversal. The way we do it is to use a stack, every time we traverse left side, we push the number into the stack. When we start to traverse the right side, we pop out the number that is smaller than current node and find its root value, which is assigned the minimum value. The idea is all the value that is smaller than minimum should already be traversed and popped out from stack, so any node that has a smaller value than the minimum is not a valid pre order traversal.


public boolean verifyPreorder(int[] preorder) {
        Stack stack = new Stack();
        int rootVal = Integer.MIN_VALUE;
        for (int n : preorder) {
            if (n < rootVal) {
                return false;
            }
            while (!stack.isEmpty() && stack.peek() < n) {
                rootVal = stack.pop();
            }
            stack.push(n);
        }
        return true;
    }

1 comment:

  1. The development of artificial intelligence (AI) has propelled more programming architects, information scientists, and different experts to investigate the plausibility of a vocation in machine learning. Notwithstanding, a few newcomers will in general spotlight a lot on hypothesis and insufficient on commonsense application. IEEE final year projects on machine learning In case you will succeed, you have to begin building machine learning projects in the near future.

    Projects assist you with improving your applied ML skills rapidly while allowing you to investigate an intriguing point. Furthermore, you can include projects into your portfolio, making it simpler to get a vocation, discover cool profession openings, and Final Year Project Centers in Chennai even arrange a more significant compensation.


    Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account.

    ReplyDelete