# Python solution with detailed inline comments

• '''

class Solution(object):

def __init__(self):

self.insert_here = []

"""
:type root: TreeNode
:type v: int
:type d: int
:rtype: TreeNode
"""
#
#  If depth d is 1 that means there is no depth d-1 at all,
# then create a tree node with value v as the new root of the whole original tree,
# and the original tree is the new root's left subtree.
#
if d == 1:
new_tree = TreeNode(v)
new_tree.left = root
return new_tree

#
# recursively traverse the tree,
# and include nodes to be updated
# in the insert_here list
#

#
# go through the insert_here list and
# add the value to insert
#
for node in self.insert_here:

#
# left
#
temp = node.left
node.left = TreeNode(v)
node.left.left = temp

#
# right
#
temp = node.right
node.right = TreeNode(v)
node.right.right = temp

#
# return the modified tree
#
return root

def addOneRowHelper(self, node, v, d, curr_depth):

#
# end recursion when there are no more nodes to traverse till depth d-1
#
if node is None:
return

#
# end recursion at depth d-1
#
if curr_depth == d - 1:

self.insert_here.append(node)

#
# recursively check left and right nodes in the next depth down
#
elif curr_depth < d: