Python recursion solution-constant space complexity


  • 0
    R
    class Solution(object):
    def __init__(self):
        self.dic={}
    def helper(self,level,root):
        if not root:
            return
        else:
            if level not in self.dic:
                self.dic[level]=root
                root.next=None
            else:
                self.dic[level].next=root
                self.dic[level]=root
                root.next=None
            self.helper(level+1,root.left)
            self.helper(level+1,root.right)
            return
    def connect(self, root):
        """
        :type root: TreeLinkNode
        :rtype: nothing
        """
        self.helper(0,root)

Log in to reply
 

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