[LeetCode][Python3] 103. Binary Tree Zigzag Level Order Traversal
2019. 2. 7. 00:25 |
프로그래밍/LeetCode
Problem :
https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/
My Solution :
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def zigzagLevelOrder(self, root: 'TreeNode') -> 'List[List[int]]':
rev = False
ret = []
if root:
queue = [root]
while queue:
level_ret = []
level_queue = []
for node in queue:
level_ret.append(node.val)
if node.left:
level_queue.append(node.left)
if node.right:
level_queue.append(node.right)
if rev:
ret.append(level_ret[::-1])
else:
ret.append(level_ret)
rev = not rev
queue = level_queue
return ret
'프로그래밍 > LeetCode' 카테고리의 다른 글
[LeetCode][Python] 341. Flatten Nested List Iterator (0) | 2019.03.22 |
---|---|
[LeetCode][Python3] 200. Number of Islands (0) | 2019.02.11 |
[LeetCode][Python3] 300. Longest Increasing Subsequence (0) | 2019.02.10 |
[LeetCode][Python3] 240. Search a 2D Matrix II (0) | 2019.02.09 |
[LeetCode][Python3] 279. Perfect Squares (0) | 2019.01.31 |
[LeetCode][Python3] 162. Find Peak Element (0) | 2019.01.25 |
[LeetCode][Python3] 328. Odd Even Linked List (0) | 2019.01.25 |
[LeetCode][Python3] 287. Find the Duplicate Number (0) | 2019.01.25 |
최근에 달린 댓글 최근에 달린 댓글