Check if a binary tree is subtree of another binary tree | Part 1 | GeeksforGeeks

preview_player
Показать описание


This video is contributed by Anant Patni

Please Like, Comment and Share the Video among your friends.

Also, Subscribe if you haven't already! :)
Рекомендации по теме
Комментарии
Автор

It can be even more optimized.
Check for tree equality only if the current node's data (main tree) and root's data(subtree) are the same.

def isSubTree(root1, root2):
if root1:
if root1.data == root2.data:
return isTreeSame(root1, root2)
return isSubTree(root1.left, root2) or isSubTree(root1.right, root2)
else:
return False

TheShantanu
Автор

Bro u just forgot to walk down the right subtree of 10

chitej