| 일 | 월 | 화 | 수 | 목 | 금 | 토 | 
|---|---|---|---|---|---|---|
| 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
													
											
												
												- 운동
 - realclass
 - 파비최
 - 미드시청
 - English
 - 프로젝트
 - Problem Solving
 - 괜찮음
 - FIT XR
 - 스탭퍼
 - 30분
 - 링피트
 - 월간
 - 리얼 클래스
 - 개발자
 - Daily Challenge
 - 만화도
 - 영어공부
 - Writing
 - 읽기
 - leetcode
 - 뭐든
 - 영어원서읽기
 - 잡생각
 - 10분
 - 3줄정리
 - 화상영어
 - 사이드
 - 쓰릴오브파이트
 - 매일
 
													Archives
													
											
												
												- Today
 
- Total
 
파비의 매일매일 공부기록
2023.12.10 Today's Challenge 본문
Construct String from Binary Tree - LeetCode
Can you solve this real interview question? Construct String from Binary Tree - Given the root of a binary tree, construct a string consisting of parenthesis and integers from a binary tree with the preorder traversal way, and return it. Omit all the empty
leetcode.com
트리 활용한 문제
class Solution:
    def tree2str(self, root: Optional[TreeNode]) -> str:
        res = []
        self.dfs(root, res)
        return ''.join(res)
    def dfs(self, t, res):
        if t is None:
            return
        res.append(str(t.val))
        if not t.left and not t.right:
            return
        res.append('(')
        self.dfs(t.left, res)
        res.append(')')
        if t.right is not None:
            res.append('(')
            self.dfs(t.right, res)
            res.append(')')반응형
    
    
    
  'Problem Solving > LeetCode' 카테고리의 다른 글
| 2023.12.12 Today's Challenge (0) | 2023.12.12 | 
|---|---|
| 2023.12.11 Today's Challenge (0) | 2023.12.11 | 
| 2023.12.09 Today's Challenge (0) | 2023.12.09 | 
| 2023.12.08 Today's Challenge (0) | 2023.12.08 | 
| 2023.12.07 Today's Challenge (0) | 2023.12.07 | 
			  Comments