leetcode Binary Tree Level Order Traversal python

python

# 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]]

"""

res=[]

if root is None:

return res

level=0

rs=self.levelOrderRe(root,level,res)

return rs

def levelOrderRe(self,tree,level,res):

if tree is None:

return res

if len(res) < level+1:

res.append([])

res[level].append(tree.val)

res=self.levelOrderRe(tree.left,level+1,res)

res=self.levelOrderRe(tree.right,level+1,res)

return res

以上是 leetcode Binary Tree Level Order Traversal python 的全部内容, 来源链接: utcz.com/z/387465.html

回到顶部