Python in-order traverse


  • 0
    class Solution(object):
        def widthOfBinaryTree(self, root):
            def inOrder(dep, rank, r):
                if not r:
                    return
                inOrder(dep + 1, 2 * (rank - 1) + 1, r.left)
                dic[dep] = dic.get(dep, [rank + 2, rank - 1])
                dic[dep][0], dic[dep][1] = min(rank, dic[dep][0]), max(rank, dic[dep][1])
                inOrder(dep + 1, 2 * (rank - 1) + 2, r.right)
            dic = {}
            inOrder(0, 1, root)
            ans = 0
            for key in dic:
                ans = max(ans, dic[key][1] - dic[key][0] + 1)
            return ans
    

Log in to reply
 

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