Walk each node in root and check if the subtree rooted there matches subRoot exactly.
function isSubtree(root, subRoot):
if root is null:
return false
if isSameTree(root, subRoot):
return true
return isSubtree(root.left, subRoot) or isSubtree(root.right, subRoot)
function isSameTree(a, b):
if a is null and b is null:
return true
if a is null or b is null:
return false
if a.val != b.val:
return false
return isSameTree(a.left, b.left) and isSameTree(a.right, b.right)
time, space.