2764. is Array a Preorder of Some Binary Tree
Given a 0-indexed integer 2D array
nodes
, your task is to determine if the given array represents the preorder traversal of some binary tree.For each index
i
,nodes[i] = [id, parentId]
, whereid
is the id of the node at the indexi
andparentId
is the id of its parent in the tree (if the node has no parent, thenparentId = -1
).Return
true
if the given array represents the preorder traversal of some tree, andfalse
otherwise.Note: the preorder traversal of a tree is a recursive way to traverse a tree in which we first visit the current node, then we do the preorder traversal for the left child, and finally, we do it for the right child.
1 | class Solution { |