leetcode-easy-trees-102. Binary Tree Level Order Traversal-YES

Posted rosyyy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode-easy-trees-102. Binary Tree Level Order Traversal-YES相关的知识,希望对你有一定的参考价值。

mycode  98.56%

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def levelOrder(self, root):
        """
        :type root: TreeNode
        :rtype: List[List[int]]
        """
        def deal(root,level):
            if not root:
                return 
            if len(self.res) <= level:
                self.res.append([root.val])
            else:
                self.res[level].append(root.val)
            deal(root.left,level+1)
            deal(root.right,level+1)
        self.res = []
        deal(root,0)
        return self.res

 

以上是关于leetcode-easy-trees-102. Binary Tree Level Order Traversal-YES的主要内容,如果未能解决你的问题,请参考以下文章