Path to given node
Apr 15, 2022
Check out the problem description here.
Solution
class Solution:
def solve(self, A, B):
ds=[]
self.rec(A,B,ds)
return ds
def rec(self,root,B,ds):
if root ==None :
return False
ds.append(root.val)
if root.val == B:
return True
if self.rec(root.left,B,ds) or self.rec(root.right,B,ds):
return True
ds.pop(-1)
return False
Time :O(N)
Space :O(h) height of the tree