Flipkart Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: Phone Interview
at step 3, you are assuming that you KNOW something that has to be discovered: whether N is in the left of right subtree. I don't see any way you can know - where'd you get that?
I guess this would work for a complete tree(i.e. tree with either two or none children). It certainly didn't work for the tree that I applied it for. BTW, please be a little more clear in your pseudo-code. I cudn't understand the "depending" stuff at all
hey u never reached the siblings of the given node...
its asked to find the left and right neighbours u are increasing the level but they will be present at same level
right??
please corret if i hav understood it wrongly
k...got u i was thikng that only the tree after level x would be given..
this one is a rather casual one
Can u explain the problem a bit more clearly, From what I understood, it is asked that we need to find the siblings(only left and right) for the given node.
The questions I have are
1. Are you given a standard binary tree with just left and right pointers?
2. If so, are you allowed pre-process the tree before you can actually find the left and right siblings?
1) Find the depth of the node, say h
2) Now, take a pointer nodePtr. Start doing DFS upto that level. Initialize nodePtr will NULL.
3) We get a node, Nh at level h, store it into nodePtr
4) get another node on level h, but this is not given node, then store this new node in nodePtr.
5) While repeating step 4, we will come to a point where we have a node N which is stored in nodePtr and just next node in the DFS on that hight h is given node, print this nodePtr node. This is left node of given node.
6) in next iteration, we will get right node of given node.