# Definition for a binary tree node # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: # @param root, a tree node # @return a list of lists of integers def zigzagLevelOrder(self, root): crt=[root] rst=[] if not crt: return [] flag=1 while crt!=[None]*len(crt): if flag==1: rst.append([i.val for i in crt]) else: temp=[i.val for i in crt] temp.reverse() rst.append(temp) nextlvl=[] for i in crt: if i.left: nextlvl.append(i.left) if i.right: nextlvl.append(i.right) crt=nextlvl flag*=-1 return rst
Friday, September 12, 2014
Leetcode: Binary Tree Zigzag Level Order Traversal @Python
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment