This post is completed by 1 user

  • 0
Add to List
Medium

78. Root-to-Node Distance in Binary Tree: How to Find

Objective: - Find The Distance From the Root To the Given Node of a binary tree.

What does Distance mean: It means several edges between two nodes.

Distance from root to given node

Approach:

  • This problem is similar to "Find Path From Root To Given Node"
  • Make the following change to that
  • Instead of returning the boolean value return the integer, nodes in the path will return the number = 1 + number returned by the previous node in the path.
  • The final return will give you the number of nodes between the root and the given leaf node so the distance will be (edges)= number of nodes -1
 

Output:

Distance from root to 45 is : 3