Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Number of Turns in Binary Tree

Hard Acceptance 50.00% Points 40.00

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

Examples
Example 1

Example 1

Input: Tree = [1, 2, 3, 4, 5, 6, 7, 8, N, 9, 10], first node = 5, second node = 10

Output: 4

Explanation: Turns will be at 2, 1, 3, 6.

Example 2

Example 2

Input: Tree = [1, 2, 3, 4, 5, 6, 7, 8, N, 9, 10], first node = 1, second node = 4

Output: 0

Explanation: No turns required

Hints
Hint 1
Expected Time Complexity: O(N)
Hint 2
Expected Auxiliary Space: O(Height of Tree)
Constraints
  • 1 = N = 10^3
Companies
Samsung
Topics
Tree
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions