일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Convert Sorted List to Binary Search Tree
- Class
- 밴픽
- Decorator
- attribute
- kaggle
- concurrency
- LeetCode
- iterator
- Python
- Regular Expression
- shiba
- Python Implementation
- t1
- 715. Range Module
- Protocol
- 프로그래머스
- Generator
- 30. Substring with Concatenation of All Words
- 컴퓨터의 구조
- Substring with Concatenation of All Words
- 시바견
- 315. Count of Smaller Numbers After Self
- 파이썬
- DWG
- 43. Multiply Strings
- data science
- Python Code
- 109. Convert Sorted List to Binary Search Tree
- 운영체제
Archives
- Today
- Total
Scribbling
LeetCode: 117. Populating Next Right Pointers in Each Node II 본문
Computer Science/Coding Test
LeetCode: 117. Populating Next Right Pointers in Each Node II
focalpoint 2021. 11. 3. 20:07"""
# Definition for a Node.
class Node:
def __init__(self, val: int = 0, left: 'Node' = None, right: 'Node' = None, next: 'Node' = None):
self.val = val
self.left = left
self.right = right
self.next = next
"""
class Solution:
def connect(self, root: 'Node') -> 'Node':
self.helper(root)
return root
def helper(self, node):
if not node:
return
leftmost, prev = None, None
cur = node
while cur:
if cur.left:
if not leftmost:
leftmost, prev = cur.left, cur.left
else:
prev.next = cur.left
prev = cur.left
if cur.right:
if not leftmost:
leftmost, prev = cur.right, cur.right
else:
prev.next = cur.right
prev = cur.right
cur = cur.next
self.helper(leftmost)
'Computer Science > Coding Test' 카테고리의 다른 글
LeetCode: 122. Best Time to Buy and Sell Stock II (0) | 2021.11.03 |
---|---|
LeetCode: 120. Triangle (0) | 2021.11.03 |
프로그래머스: 징검다리 (0) | 2021.11.03 |
프로그래머스: 입국심사 (0) | 2021.11.02 |
LeetCode: 116. Populating Next Right Pointers in Each Node (0) | 2021.11.02 |