The test cases are weak. 2 different trees give the same preorder traversal

interview-questions
Tags: #<Tag:0x00007f182875dba8>

#1

The testcases are weak. If i perform preorder traversal for both, it gives the correct ans, though that shoudl not be the case. Eg: 7 1 2 -1 3 -1 -1 and 7 1 -1 2 -1 -1 3 will have same preorder but they are different trees.


#2

2 different trees can give same preorder traversal, which is why you need to do either inorder or postorder to differentiate.


#3

are you first doing preorder traversal of both the trees and then comparing the sequence?
there is no need for that we can do this even in a preorder state checking at each point if one node has become null and other has not then they are not equal and if the current node’s data is not equal to other node’s then it is not equal.
return & of left and right recursive calls