840. Is Array a Preorder of Some Binary Tree
Your objective is to determine whether a particular array represents the preorder traversal of a binary tree, given a 0-indexed integer 2D array of nodes.
For each index i, nodes[i] = [id, parentId], where id is the id of the node at the index i and parentId is the id of its parent in the tree (if the node has no parent, then parentId == -1).
Return false otherwise, and true if the array provided represents the preorder traversal of a tree.
Note : A tree can be traversed recursively using the preorder traversal method, which involves visiting the current node first, then performing the preorder traversal for the left child, and lastly performing the same for the right child.
Example 1:
Input : nums = [[0,-1], [1,0], [2, 1], [3, 0], [4, 3], [5, 3]]
Output : true
Explanation :
The given nodes make the tree in the picture below.
We can show that this is the preorder traversal of the tree, first we visit node 0, then we do the preorder traversal of the left child which is [1], then we do the preorder traversal of the left child which is [2]. Then we go for right child [3] for preorder traversal which is [3, 4, 5].

Example 2:
Input : nums = [[0,-1], [1,0], [3, 0], [2, 1], [5, 3], [4, 3]]
Output : false
Explanation :
The given nodes make the tree in the picture below.
For the preorder traversal, first we visit node 0, then we do the preorder traversal of the left child which is [1, 2], but we can see that in the given order, 3 comes between 1 and 2, so, it's not the preorder traversal of the tree.

Now Your Turn!
Pick the correct output for the given inputInput : nums = [[0,-1],[1,0],[2,0],[3,2],[4,2]]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 <= nodes.length <= 105
- nodes[i].length == 2
- 0 <= nodes[i][0] <= 105
- -1 <= nodes[i][1] <= 105
- The input is generated such that nodes make a binary tree.