Interview Question
Country: United States
Can you please restate the problem:
Do you want to know, if there is a subtree D in the tree T that has value x in the root of D and value y somewhere in D. If you write binary tree you assume no BST (no order)?
In this case something like:
def find(root, values, i):
if root is None: return False
if root.value == values[i]: i = i - 1
if i < 0: return True
return find(root.left, v, i) or find(root.right, v, i)
def find_if_descendant(root, x, y):
return find(root, [x, y], 1)
- NoOne August 16, 2017