Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 5765 |

@blackshadows | 5715 |

@akhayrutdinov | 5111 |

@mb1973 | 4993 |

@Quandray | 4944 |

@saiujwal13083 | 4506 |

@sanjay05 | 3762 |

@marius_valentin_dragoi | 3516 |

@sushant_a | 3459 |

@verma_ji | 3341 |

@KshamaGupta | 3318 |

Complete Leaderboard | |

Handle | Score |

@aroranayan999 | 1276 |

@bt8816103042 | 739 |

@rohitanand | 495 |

@codeantik | 479 |

@shalinibhataniya1097 | 472 |

@amrutakashikar2 | 464 |

@thealchemist627 | 456 |

@akashkale117 | 453 |

@shivanandp12345678910 | 453 |

@dalwainazism125 | 453 |

@SherlockHolmes3 | 447 |

Complete Leaderboard |

Given a binary tree and data value of two of its nodes. Find the number of turns needed to reach from one node to another in the given binary tree.

**Example 1:**

Input :Tree = 1 / \ 2 3 / \ / \ 4 5 6 7 / / \ 8 9 10 first node = 5 second node = 10Output:4Explanation :Turns will be at 2, 1, 3, 6.

**Example 2:**

Input :Tree = 1 / \ 2 3 / \ / \ 4 5 6 7 / / \ 8 9 10 first node = 1 second node = 4Output :-1Explanation:No turn is required since they are in a straight line.

**Your Task: **

You don't need to read input or print anything. Complete the function **NumberOFTurns()** which takes root node and data value of 2 nodes as input parameters and returns number of turns required to navigate between them. If the two nodes are in a straight line, ie- the path does not involve any turns, return -1.

**Expected Time Complexity:** O(N)

**Expected Auxiliary Space: **O(Height of Tree)

**Constraints:**

1 ≤ N ≤ 10^3

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes
Number of Turns in Binary Tree

...