I have some code to traverse a binary tree in-order recursively.
def IN_DFS(node, result=None):
if result is None:
result = []
if not node:
return result
Solution.IN_DFS(node.left, result)
result.append(node.val)
Solution.IN_DFS(node.right, result)
return result
I need some help understanding what is happening. I understand recursion, and I know how to traverse a binary tree in-order iteratively, but can't seem to see what is happening for this recursive solution.
So if 'node' is not None, we are calling the recursive function on node.left until we reach a lead node, in which case node.left is None, and we move to the next line 'result.append(node.val)'? - is this right?
Then the recursive function is called on 'node.right'?
So say we reach the first node.left leaf node, add its value to 'result' then go back up one step to its parent, add its value to 'result', then check its right child?
I don't think the above is right- anyone can help? Thanks