Given a binary tree and two nodes, the task is to Print all the nodes that are common for 2 given nodes in a binary tree.
Given binary tree is : 1 / \ 2 3 / \ / \ 4 5 6 7 / / \ 8 9 10 Given nodes 9 and 7, so the common nodes are:- 1, 3
Asked in : Amazon
- Find the LCA of given two nodes.
- Print all ancestors of the LCA as done in this post, also print the LCA.
This article is contributed by Sahil Chhabra. 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 path common to the two paths from the root to the two given nodes
- Print path from root to all nodes in a Complete Binary Tree
- Tarjan's off-line lowest common ancestors algorithm
- Count of nodes that are greater than Ancestors
- Find if there is a pair in root to a leaf path with sum equals to root's data
- Print Ancestors of a given node in Binary Tree
- Sum of nodes on the longest path from root to leaf node
- Root to leaf path with maximum distinct nodes
- Find the path from root to the given nodes of a tree for multiple queries
- Sum of Nodes and respective Neighbors on the path from root to a vertex V
- Find node U containing all nodes from a set V at atmost distance 1 from the path from root to U
- Print path from root to a given node in a binary tree
- Print the first shortest root to leaf path in a Binary Tree
- Print the longest path from root to leaf in a Binary tree
- Iterative method to find ancestors of a given binary tree
- Print nodes at k distance from root
- Print nodes at k distance from root | Iterative
- Sum of all odd nodes in the path connecting two given nodes
- Common nodes in the inorder sequence of a tree between given two nodes in O(1) space
- Print path between any two nodes in a Binary Tree