short python dfs


  • 0
    S
    def addOneRow(self, root, v, d):
    
    	dummy = TreeNode(0)
    	dummy.left = root
    	stackk = [(dummy, 0)]
    	while stackk:
    		(node, level) = stackk.pop()
    		if not node or level > d - 1:
    			continue
    		if level < d - 1:
    			stackk.append((node.right, level+1))
    			stackk.append((node.left, level+1))
    		else: #level == d - 1:
    			rnode = node.right
    			node.right = TreeNode(v)
    			newrnode.right = rnode
    			
    			lnode = node.left
    			node.left = TreeNode(v)
    			newlnode.left = lnode
    	return dummy.left
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.