일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
Tags
- 시바견
- 109. Convert Sorted List to Binary Search Tree
- 43. Multiply Strings
- Class
- Python Implementation
- attribute
- Python
- 파이썬
- concurrency
- 315. Count of Smaller Numbers After Self
- 30. Substring with Concatenation of All Words
- Protocol
- data science
- t1
- Convert Sorted List to Binary Search Tree
- Regular Expression
- 운영체제
- 밴픽
- DWG
- 컴퓨터의 구조
- Decorator
- iterator
- Generator
- 715. Range Module
- Substring with Concatenation of All Words
- kaggle
- Python Code
- 프로그래머스
- shiba
- LeetCode
Archives
- Today
- Total
Scribbling
LeetCode: 103. Binary Tree Zigzag Level Order Traversal 본문
Computer Science/Coding Test
LeetCode: 103. Binary Tree Zigzag Level Order Traversal
focalpoint 2021. 10. 20. 16:21102번 문제를 풀면 하나 더 주는 공짜 문제.
밑의 코드만 추가해준다.
for i in range(len(ret)):
if i % 2 == 1:
ret[i].reverse()
https://focalpoint.tistory.com/128
from collections import deque
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def zigzagLevelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
if not root:
return []
ret = []
q = deque()
q.append((root, 0))
while q:
cur_node, cur_level = q.popleft()
if len(ret) == cur_level:
ret.append([cur_node.val])
else:
ret[cur_level].append(cur_node.val)
if cur_node.left != None:
q.append((cur_node.left, cur_level+1))
if cur_node.right != None:
q.append((cur_node.right, cur_level+1))
for i in range(len(ret)):
if i % 2 == 1:
ret[i].reverse()
return ret
'Computer Science > Coding Test' 카테고리의 다른 글
프로그래머스: 큰 수 만들기 (0) | 2021.10.20 |
---|---|
LeetCode: 105. Construct Binary Tree from Preorder and Inorder Traversal (0) | 2021.10.20 |
LeetCode: 102. Binary Tree Level Order Traversal (0) | 2021.10.20 |
프로그래머스: 모의고사 (0) | 2021.10.20 |
프로그래머스: 카펫 (0) | 2021.10.19 |