educative.io

Educative

Solution for checking if index != 0 and node.val != seq[index]

Why arent we checking for -> node.val != seq[index] && index != 0 condition? For example, if the sequence is 1,2, 3 and the root’s value inst 1, we should still explore the remainder of the tree because the sequence can start further down after the root/or any node .

If (node.val != seq[index] && index != 0 ) return false // because we are trying to find index > 0 now whereas if index == 0, means we havent found the first element from our sequence yet, so we should continue exploring.

reread the question and I see that the path has to be from root to leaf so the condition which I am proposing is unnecessary.