일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 31 |
Tags
- 컴퓨터의 구조
- 프로그래머스
- 43. Multiply Strings
- Regular Expression
- 715. Range Module
- 30. Substring with Concatenation of All Words
- LeetCode
- DWG
- Protocol
- 315. Count of Smaller Numbers After Self
- data science
- attribute
- 운영체제
- Substring with Concatenation of All Words
- Convert Sorted List to Binary Search Tree
- concurrency
- Class
- Python
- shiba
- 109. Convert Sorted List to Binary Search Tree
- 시바견
- Python Implementation
- Python Code
- t1
- iterator
- kaggle
- 밴픽
- Decorator
- Generator
- 파이썬
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 |