【leetcode❤python】102. Binary Tree Level Order Traversal
#-*- coding: UTF-8 -*-
#广度优先遍历
# 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):
tmplist1=[]
def dfsDepth(self,root):
if root==None:return 0
leftDepth=self.dfsDepth(root.left)
rightDepth=self.dfsDepth(root.right)
return leftDepth+1 if leftDepth >rightDepth else (rightDepth+1)
def levelOrder(self, root):
"""
:type root: TreeNode
:rtype: List[List[int]]
"""
self.tmplist1=[]
depth=self.dfsDepth(root)
result=[]
for level in xrange(0,depth):
self.getlevel(root,level)
result.append(self.tmplist1)
self.tmplist1=[]
return result
def getlevel(self,root,level):
if root==None:return
if level==0:
self.tmplist1.append(root.val)
else:
self.getlevel(root.left,level-1)
self.getlevel(root.right,level-1)