Sharing VERY SIMPLE Python recursive solution


  • 1
    class Solution(object):
        def levelOrder(self, root):
            
            def _level(root, level, dict_levels):
                if root is None:
                    return
                dict_levels[level] = dict_levels.get(level, []) + [root.val]
                _level(root.left, level + 1, dict_levels)
                _level(root.right, level + 1, dict_levels)
                
            d = {}
            _level(root, 0, d)
            return d.values()

  • 0
    K

    Thanks. Really explict.


Log in to reply
 

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