파비의 매일매일 공부기록

Today's Challenge 본문

Problem Solving/LeetCode

Today's Challenge

fabichoi 2022. 9. 7. 23:45

https://leetcode.com/problems/construct-string-from-binary-tree/

 

Construct String from Binary Tree - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

오늘도 트리 문제. Easy 난이도인데 쉬운지는 잘 모르겄다 =_=;

class Solution:
    def tree2str(self, root: Optional[TreeNode]) -> str:
        if not root:
            return ''
        
        output = str(root.val)
        
        if root.left or root.right:
            output += '(' + self.tree2str(root.left) + ')'
        
        if root.right:
            output += '(' + self.tree2str(root.right) + ')'
        
        return output
반응형

'Problem Solving > LeetCode' 카테고리의 다른 글

Today's Challenge  (0) 2022.09.09
Today's Challenge  (0) 2022.09.08
Today's Challenge  (0) 2022.09.06
Today's Challenge  (0) 2022.09.05
Today's Challenge  (0) 2022.09.04
Comments