Ezvor

2764. Is Array a Preorder of Some ‌Binary Tree

MediumStack Preparing judge… LeetCode
StackTreeDepth-First SearchBinary Tree

Hints

Hint 1
Think of using the stack data structure.
Hint 2
Put the first node in the stack.
Hint 3
Iterate over the array and check if the node at the top of the stack is its parent; if it’s not, then pop the last element of the stack and check until you reach the parent of the current node in the array.
Hint 4
If the stack gets empty at any point, then the array is not the preorder.