| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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
- Spark
- docker
- 2023년 목표
- Dynamic Programming
- Data Engineering
- telegram
- 빅데이터를 지탱하는 기술
- Airflow
- BOT
- dsf
- data_engineer
- 데이터 엔지니어
- SQL
- 프로그래머스
- Pseudo Lab
- docker container
- leetcode
- docker image
- airflow webserver
- 백준 온라인 저지
- datacamp
- delete join
- airflow architecture
- 그리디
- terraform
- MySQL
- hackerrank
- 백준온라인저지
- 알고리즘
- Python
Archives
- Today
- Total
Lim Seunghyun Space
[LeetCode] 513. Find Bottom Left Tree Value 본문
문제 출처
문제 풀이
- 이진 트리가 주어질 때, 트리의 가장 마지막 줄의 가장 왼쪽 값을 반환하는 함수를 작성하는 문제
- 트리를 BFS처럼 순회하여 마지막 레벨을 찾고 마지막 레벨에서 가장 왼쪽 값을 찾아낸다.
- Deque를 이용하여 루트부터 읽고 다음 레벨의 노드를 순회하며, 이때 Left 노드와 Right 노드가 있을 때 Right 노드부터 Deque에 추가해야 가장 왼쪽 값을 찾을 수 있다.
나의 풀이(Python3)
from collections import deque
from typing import Optional
# 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 findBottomLeftValue(self, root: Optional[TreeNode]) -> int:
deq = deque([root])
last = None
while deq:
value = deq.popleft()
last = value.val # store popped node value
left, right = value.left, value.right
if right:
deq.append(right)
if left:
deq.append(left)
return last
Github
728x90
'Algorithm > 문제' 카테고리의 다른 글
| [Leetcode] First Bad Version (0) | 2022.12.05 |
|---|---|
| [Leetcode] Valid Perfect Sqaure (0) | 2022.11.26 |
| [Leetcode] Peak Index in a Mountain Array (0) | 2022.11.26 |
| [Leetcode] Search Insert Position (0) | 2022.11.24 |
| [Leetcode] Guess Number Higher or Lower (0) | 2022.11.24 |