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 Common Nodes in Two Binary Search Trees
- Tarjan's off-line lowest common ancestors algorithm
- Print path from root to all nodes in a Complete Binary Tree
- Common nodes in the inorder sequence of a tree between given two nodes in O(1) space
- Sum of nodes on the longest path from root to leaf node
- Sum of Nodes and respective Neighbors on the path from root to a vertex V
- Root to leaf path with maximum distinct nodes
- Lowest Common Ancestor for a Set of Nodes in a Rooted Tree
- Least Common Ancestor of any number of nodes in Binary Tree
- Find the path from root to the given nodes of a tree for multiple queries
- Print path from root to a given node in a binary tree
- Print nodes at k distance from root
- Print the longest path from root to leaf in a Binary tree
- Print the first shortest root to leaf path in a Binary Tree
- Print nodes at k distance from root | Iterative
- Print path between any two nodes in a Binary Tree | Set 2
- Print path between any two nodes in a Binary Tree
- 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