Given a Binary Tree with distinct nodes. Given two nodes node1 and node2, check if the two nodes lies in the same subtree of the root node. That is, either of the left and right subtrees of the root node.
For Example: In the above binary tree, node 3 and 8 are in the same subtree but 4 and 5 are in different subtree.
Prerequisite: Check if a node exist in Binary Tree.
The idea is similar of searching a node in Binary Tree. There are four different cases:
- If both node1 and node2 are in left subtree of root node.
- If both node1 and node2 are in right subtree of the root node.
- If node1 is in the left subtree of the root node and node2 is in the right subtree of root node.
- If node1 is in the right subtree of the root node and node2 is in the left subtree of root node.
Use the approach of searching a node in Binary Tree and check if any of the first two cases listed above is True. If any of the first two cases listed above is found True then print YES otherwise print NO.
Below is the implementation of the above approach:
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.
- Minimum number of Nodes to be removed such that no subtree has more than K nodes
- Change a Binary Tree so that every node stores sum of all nodes in left subtree
- Convert a Binary Tree such that every node stores the sum of all nodes in its right subtree
- Number of leaf nodes in the subtree of every node of an n-ary tree
- Queries to find the Minimum Weight from a Subtree of atmost D-distant Nodes from Node X
- Node having maximum number of nodes less than its value in its subtree
- Print the path common to the two paths from the root to the two given nodes
- Subtree of all nodes in a tree using DFS
- Count of nodes having odd divisors in the given subtree for Q queries
- Queries for M-th node in the DFS of subtree
- Find the Kth node in the DFS traversal of a given subtree in a Tree
- Find GCD of each subtree of a given node in an N-ary Tree for Q queries
- Sum of nodes on the longest path from root to leaf node
- Find node U containing all nodes from a set V at atmost distance 1 from the path from root to U
- Find if there is a pair in root to a leaf path with sum equals to root's data
- Check if a binary tree is subtree of another binary tree | Set 1
- Check if a binary tree is subtree of another binary tree | Set 2
- Check if the given Binary Tree have a Subtree with equal no of 1's and 0's
- Check if a binary tree is subtree of another binary tree using preorder traversal : Iterative
- Check if two nodes are on same path in a tree | Set 2
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.