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.
- 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
- Print Common Nodes in Two Binary Search Trees
- Tarjan's off-line lowest common ancestors algorithm
- Root to leaf path with maximum distinct nodes
- Sum of nodes on the longest path from root to leaf node
- 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 nodes at k distance from root
- Print nodes at k distance from root | Iterative
- Print path between any two nodes in a Binary Tree
- 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
- GCD from root to leaf path in an N-ary tree
- Root to leaf path sum equal to a given number