I am trying to run this code for search an element in a binary tree. It works well but I cannot return the value but can display it.
Here is my code:
def getNodeAddress(self, data):
print(self._getNodeAddress(data, self.root))
def _getNodeAddress(self, data, root):
if root:
if root.data == data:
print("<<<< ", root.data)
return root
self._getNodeAddress(data, root.left)
self._getNodeAddress(data, root.right)
def _getNodeAddress(self, data, root, res):
if root:
if root.data == data:
print("<<<< ", root.data)
res = root
self._getNodeAddress(data, root.left, res)
self._getNodeAddress(data, root.right, res)
print(res)
return res
So in the above method, it traverses the tree to look for the value as the variable key, when it finds one it prints as in the line "print("<<<< ", root.data)", which does display as expected. But now the issue arises when I need to return the value to the parent method which is "getNodeAddress". I cannot print the value in the parent method. It just print "None" So my question is how do I return the value from a recursion.
I also tried the second approach as in the above code of the same method name, but still exactly the same the issue Any help would really be appreciated