Closed ManiAm closed 2 years ago
Since you are using the custom class. You must implement the __repr__
.
For simplicity, let's implement the repr to return the string representation of value
class tree_node():
def __init__(self, val, left=None, right=None):
self.val = val
self.left = left
self.right = right
def __repr__(self):
return str(self.val)
Then the tree looks as follows for dfs(root, 11)
Let's say we want dfs(root, 10)
, the tree looks sth like this
Thanks!
I am using this code snippet to perform DFS on a binary tree. The target value does not exist in the tree, so I am expecting to traverse all the nodes.
However, the DFS recursion tree that is generated does not look correct,
Am I missing something here?