일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- Generator
- iterator
- 컴퓨터의 구조
- Class
- t1
- Python
- concurrency
- 715. Range Module
- attribute
- Decorator
- 시바견
- 프로그래머스
- Python Code
- 43. Multiply Strings
- Regular Expression
- shiba
- Substring with Concatenation of All Words
- 30. Substring with Concatenation of All Words
- 315. Count of Smaller Numbers After Self
- Python Implementation
- data science
- 운영체제
- kaggle
- 파이썬
- LeetCode
- DWG
- Convert Sorted List to Binary Search Tree
- 밴픽
- 109. Convert Sorted List to Binary Search Tree
- Protocol
Archives
- Today
- Total
Scribbling
LeetCode: 114. Flatten Binary Tree to Linked List 본문
Computer Science/Coding Test
LeetCode: 114. Flatten Binary Tree to Linked List
focalpoint 2021. 11. 2. 14:13현재 node -> Flattened Left Tree -> Flattened Right Tree
# 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 flatten(self, root: Optional[TreeNode]) -> None:
"""
Do not return anything, modify root in-place instead.
"""
if not root:
return None
head, tail = self.helper(root)
return head
def helper(self, node):
if node.left and node.right:
ls, le = self.helper(node.left)
rs, re = self.helper(node.right)
node.left = ls.left = rs.left = None
node.right = ls
le.right = rs
return [node, re]
if node.left:
ls, le = self.helper(node.left)
node.left = ls.left = None
node.right = ls
return [node, le]
if node.right:
rs, re = self.helper(node.right)
node.left = rs.left = None
return [node, re]
return [node, node]
'Computer Science > Coding Test' 카테고리의 다른 글
프로그래머스: 입국심사 (0) | 2021.11.02 |
---|---|
LeetCode: 116. Populating Next Right Pointers in Each Node (0) | 2021.11.02 |
프로그래머스: 여행경로 (0) | 2021.11.01 |
프로그래머스: 단어 변환 (0) | 2021.11.01 |
프로그래머스: 네트워크 (0) | 2021.10.31 |