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:
“””Python3 program to check if two nodes
are in same subtrees of the root node “””
# A Binary Tree Node
# Utility function to create a
# new tree node
# Constructor to create a newNode
def __init__(self, data):
self.left = None
self.right = None
self.visited = False
# Function to traverse the tree in
# preorder and check if the given
# node exists in a binary tree
def ifNodeExists(node, key) :
if (node == None):
if (node.data == key):
“”” then recur on left sutree “””
res1 = ifNodeExists(node.left, key)
“”” now recur on right subtree “””
res2 = ifNodeExists(node.right, key)
return res1 or res2
# Function to check if the two given nodes
# are in same subtrees of the root node
def ifSameSubTree(root, node1, node2):
if (root == None) :
# CASE 1: If both nodes are in
# left subtree
if (ifNodeExists(root.left, node1)
and ifNodeExists(root.left, node2)):
# CASE 2: If both nodes are in
# right subtree
elif (ifNodeExists(root.right, node1)
and ifNodeExists(root.right, node2)):
# CASE 3 and 4: Nodes are in
# different subtrees
# Driver Code
if __name__ == ‘__main__’:
root = newNode(0)
root.left = newNode(1)
root.left.left = newNode(3)
root.left.left.left = newNode(7)
root.left.right = newNode(4)
root.left.right.left = newNode(8)
root.left.right.right = newNode(9)
root.right = newNode(2)
root.right.left = newNode(5)
root.right.right = newNode(6)
node1 = 3
node2 = 8
if (ifSameSubTree(root, node1, node2)):
# This code is contributed by
- Number of leaf nodes in the subtree of every node of an n-ary tree
- Convert a Binary Tree such that every node stores the sum of all nodes in its right subtree
- Change a Binary Tree so that every node stores sum of all nodes in left subtree
- Sum of nodes on the longest path from root to leaf node
- Queries for M-th node in the DFS of subtree
- Subtree of all nodes in a tree using DFS
- Find the Kth node in the DFS traversal of a given subtree in a Tree
- Find root of the tree where children id sum for every node is given
- Check if the given Binary Tree have a Subtree with equal no of 1's and 0's
- Print nodes at k distance from root
- Find distance from root to given node in a binary tree
- Iterative program to find distance of a node from root
- Print path from root to a given node in a binary tree
- Sort the path from root to a given node in a Binary Tree
- Count of nodes which are at a distance X from root and leaves
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.