def rebuild_node(doctype, parent, left, parent_field):
	from frappe.utils import now
	n = now()

	# the right value of this node is the left value + 1
	right = left+1

	# get all children of this node
	result = frappe.db.sql("SELECT name FROM `tab%s` WHERE `%s`=%s" %
		(doctype, parent_field, '%s'), (parent))
	for r in result:
		right = rebuild_node(doctype, r[0], right, parent_field)

	# we've got the left value, and now that we've processed
	# the children of this node we also know the right value
	frappe.db.sql("""UPDATE `tab{0}` SET lft=%s, rgt=%s, modified=%s
		WHERE name=%s""".format(doctype), (left,right,n,parent))

	#return the right value of this node + 1
	return right+1
reset lft, rgt and recursive call for all children
dataset/ETHPy150Open frappe/frappe/frappe/utils/nestedset.py/rebuild_node