Given a binary tree and two nodes. The task is to count the number of turns needs to reach from one node to another node of the Binary tree.
Input: Below Binary Tree and two nodes 5 & 6 1 / \ 2 3 / \ / \ 4 5 6 7 / / \ 8 9 10 Output: Number of Turns needed to reach from 5 to 6: 3 Input: For above tree if two nodes are 1 & 4 Output: Straight line : 0 turn
Idea based on the Lowest Common Ancestor in a Binary Tree
We have to follow the step.
1…Find the LCA of given two node
2…Given node present either on the left side or right side or equal to LCA.
…. According to above condition over program falls under two Case.
Case 1: If none of the nodes is equal to LCA, we get these nodes either on the left side or right side. We call two functions for each node. ....a) if (CountTurn(LCA->right, first, false, &Count) || CountTurn(LCA->left, first, true, &Count)) ; ....b) Same for second node. ....Here Count is used to store number of turns need to reached the target node. Case 2: If one of the nodes is equal to LCA_Node. Then we count only number of turns needs to reached the second node. If LCA == (Either first or second) ....a) if (countTurn(LCA->right, second/first, false, &Count) || countTurn(LCA->left, second/first, true, &Count)) ;
3… Working of CountTurn Function
// we pass turn true if we move
// left subtree and false if we
// move right subTree
CountTurn(LCA, Target_node, count, Turn) // if found the key value in tree if (root->key == key) return true; case 1: If Turn is true that means we are in left_subtree If we going left_subtree then there is no need to increment count else Increment count and set turn as false case 2: if Turn is false that means we are in right_subtree if we going right_subtree then there is no need to increment count else increment count and set turn as true. // if key is not found. return false;
Below the implementation of above idea.
Time Complexity : O(n)
This article is contributed by Nishant Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Print the number of set bits in each node of a Binary Tree
- Kth ancestor of a node in binary tree | Set 2
- Get Level of a node in a Binary Tree
- Search a node in Binary Tree
- K-th ancestor of a node in Binary Tree
- K-th ancestor of a node in Binary Tree | Set 3
- Closest leaf to a given node in Binary Tree
- Find the Deepest Node in a Binary Tree
- Inorder Successor of a node in Binary Tree
- Delete the last leaf node in a Binary Tree
- Print cousins of a given node in Binary Tree
- Find the parent of a node in the given binary tree
- Get last node of the binary tree following given pattern starting from X
- Postorder successor of a Node in Binary Tree
- Preorder predecessor of a Node in Binary Tree
- Total sum except adjacent of a given node in a Binary Tree
- Preorder Successor of a Node in Binary Tree
- Print Ancestors of a given node in Binary Tree
- Get maximum left node in binary tree
- Kth node in Diagonal Traversal of Binary Tree