您的位置:首页 > 其它

LintCode:二叉树的层次遍历 II

2016-05-13 13:19 399 查看
LintCode:二叉树的层次遍历 II

"""
Definition of TreeNode:
class TreeNode:
def __init__(self, val):
self.val = val
self.left, self.right = None, None
"""

class Solution:
"""
@param root: The root of binary tree.
@return: buttom-up level order in a list of lists of integers
"""
def levelOrderBottom(self, root):
# write your code here
L = []
if root == None:
return []
L.append([root.val])
L.append([])
if root.left != None:
L[1].append(root.left)
if root.right != None:
L[1].append(root.right)
m = 2

while len(L[1]) != 0:
n = len(L[1])
L.append([])
for i in range(n):
if L[1][0].left != None:
L[1].append(L[1][0].left)
if L[1][0].right != None:
L[1].append(L[1][0].right)
L[m].append(L[1][0].val)
L[1].remove(L[1][0])
m += 1
L.remove(L[1])
return L[::-1]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: