A more elegant way to return a list of node values from depth-first recursive function in Python 3?
Question:
I’m working out how to return a list of values from a depth-first traversal of a binary tree. Specifically, I’m trying to do so with a recursive function.
After playing around a bit I got this code to properly return the list of expected values, but using a try/finally block in order to pull it off seems clunky to me.
def depth_first_recursive(root, node_list: list):
try:
if not root:
return
else:
node_list.append(root.val)
depth_first_recursive(root.left, node_list)
depth_first_recursive(root.right, node_list)
finally:
return node_list
class Node:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
values = []
if __name__ == '__main__':
a = Node('a')
b = Node('b')
c = Node('c')
d = Node('d')
e = Node('e')
f = Node('f')
a.left = b
a.right = c
b.left = d
b.right = e
c.right = f
nodelist = []
print(depth_first_recursive(a, node_list=nodelist))
returns ['a', 'b', 'd', 'e', 'c', 'f', ]
Is there a better way to pull this off in Python?
Answers:
I have applied the same logic as yours but tried to make code compact:
def depth_first_recursive(root, node_list: list):
if root is None: return []
return [root.val]+depth_first_recursive(root.left, node_list) + depth_first_recursive(root.right, node_list)
Output:
[‘a’, ‘b’, ‘d’, ‘e’, ‘c’, ‘f’]
Don’t need to maintain a node_list
, just have more faith in the structure of your recursion.
def depth_first_recursive(root):
if root is None:
return []
# Terminal case when reaching bottom of tree
elif not root.left and not root.right:
return [root.val]
# Get current value and continue to append list
else:
return [root.val] + depth_first_recursive(root.left) + depth_first_recursive(root.right)
I’m working out how to return a list of values from a depth-first traversal of a binary tree. Specifically, I’m trying to do so with a recursive function.
After playing around a bit I got this code to properly return the list of expected values, but using a try/finally block in order to pull it off seems clunky to me.
def depth_first_recursive(root, node_list: list):
try:
if not root:
return
else:
node_list.append(root.val)
depth_first_recursive(root.left, node_list)
depth_first_recursive(root.right, node_list)
finally:
return node_list
class Node:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
values = []
if __name__ == '__main__':
a = Node('a')
b = Node('b')
c = Node('c')
d = Node('d')
e = Node('e')
f = Node('f')
a.left = b
a.right = c
b.left = d
b.right = e
c.right = f
nodelist = []
print(depth_first_recursive(a, node_list=nodelist))
returns ['a', 'b', 'd', 'e', 'c', 'f', ]
Is there a better way to pull this off in Python?
I have applied the same logic as yours but tried to make code compact:
def depth_first_recursive(root, node_list: list):
if root is None: return []
return [root.val]+depth_first_recursive(root.left, node_list) + depth_first_recursive(root.right, node_list)
Output:
[‘a’, ‘b’, ‘d’, ‘e’, ‘c’, ‘f’]
Don’t need to maintain a node_list
, just have more faith in the structure of your recursion.
def depth_first_recursive(root):
if root is None:
return []
# Terminal case when reaching bottom of tree
elif not root.left and not root.right:
return [root.val]
# Get current value and continue to append list
else:
return [root.val] + depth_first_recursive(root.left) + depth_first_recursive(root.right)