Traverse through binary tree to change the value of each nodes in python

def(node, 2):
Before:
      1
     / \
    2   3
   /\   /
  5  6  7 
After:
      3
     /\
    4  5
   /\  /
  7  8 9
How should I approach this recursively?!


def add_value(node, delta):
    node.value += delta
    for child in node.children:
        add_value(child, delta)

No comments:

Post a Comment