Python DFS


  • 0
    class Solution:
        def connect(self, root):
            dic = {}
            def dfs(r, dep):
                if not r:
                    return
                if dep in dic:
                    dic[dep].next = r
                r.next = None
                dic[dep] = r
                dfs(r.left, dep + 1)
                dfs(r.right, dep + 1)
            dfs(root, 0)
    

Log in to reply
 

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