위지원의 데이터 일기 🐈
Home
  • 분류 전체보기 (563)
    • ✎ 2025년 (4)
    • 2024년 (16)
    • 2023년 (6)
    • 2022년 (35)
      • Developement (22)
      • Error (9)
    • 2021년 (68)
      • ERROR (9)
      • 알고리즘 (11)
      • 개발공부 (21)
      • Data (15)
      • 21.下 (12)
    • 2020년 (164)
      • 코테 (84)
      • Development (29)
      • 정처기 (41)
    • 2019년 (27)
    • 2018년 (89)
      • English Speaking (8)
      • Error (12)
      • C, Java, FileSystem (13)
      • DataBase (15)
      • Java (2)
      • 지식 (16)
      • Go (3)
      • spark (9)
      • 영어 (5)
      • 알고리즘 (6)
    • 2017년 (143)
      • Error (17)
      • machine learning (16)
      • Spark (20)
      • Database (19)
      • Python (17)
      • Spring (9)
      • etc. (10)
      • 백준 (5)
      • Google Platform (12)
      • web Development (7)
      • Docker (3)
      • Linux (8)
Home
  • 분류 전체보기 (563)
    • ✎ 2025년 (4)
    • 2024년 (16)
    • 2023년 (6)
    • 2022년 (35)
      • Developement (22)
      • Error (9)
    • 2021년 (68)
      • ERROR (9)
      • 알고리즘 (11)
      • 개발공부 (21)
      • Data (15)
      • 21.下 (12)
    • 2020년 (164)
      • 코테 (84)
      • Development (29)
      • 정처기 (41)
    • 2019년 (27)
    • 2018년 (89)
      • English Speaking (8)
      • Error (12)
      • C, Java, FileSystem (13)
      • DataBase (15)
      • Java (2)
      • 지식 (16)
      • Go (3)
      • spark (9)
      • 영어 (5)
      • 알고리즘 (6)
    • 2017년 (143)
      • Error (17)
      • machine learning (16)
      • Spark (20)
      • Database (19)
      • Python (17)
      • Spring (9)
      • etc. (10)
      • 백준 (5)
      • Google Platform (12)
      • web Development (7)
      • Docker (3)
      • Linux (8)
블로그 내 검색
포트폴리오

위지원의 데이터 일기 🐈

데이터를 사랑하고 궁금해하는 기록쟁이입니다! 😉 Super Data Girl이 되는 그날까지🏃‍♀️ 화이팅!

  • 🖥 깃블로그
  • 🌍 위키원
  • 📑 내맘대로 스크랩
  • 💌 메일
  • 2020년/코테

    [코테 연습] Sum of Root To Leaf Binary Numbers

    2020. 9. 9. 14:43

    by. 위지원

    https://leetcode.com/explore/challenge/card/september-leetcoding-challenge/555/week-2-september-8th-september-14th/3453/

     

    Explore - LeetCode

    LeetCode Explore is the best place for everyone to start practicing and learning on LeetCode. No matter if you are a beginner or a master, there are always new topics waiting for you to explore.

    leetcode.com

     

    Given a binary tree, each node has value 0 or 1.  Each root-to-leaf path represents a binary number starting with the most significant bit.  For example, if the path is 0 -> 1 -> 1 -> 0 -> 1, then this could represent 01101 in binary, which is 13.

    For all leaves in the tree, consider the numbers represented by the path from the root to that leaf.

    Return the sum of these numbers.

     


    푸는데 걸린 시간:30분
    class Solution:
        total = []
        def dfs(self, res, node):
            if node.val == -1:
                return 
            
            res+=str(node.val)
            node.val = -1
            
            if not node.left and not node.right:
                exp = 0
                decimal = 0
                for num in res[::-1]:
                    decimal =+ decimal+int(num) * (2**exp)
                    exp +=1
                    
                self.total.append(decimal)
            
            if node.left:
                self.dfs(res, node.left)
            if node.right:
                self.dfs(res, node.right)
                
        def sumRootToLeaf(self, root: TreeNode) -> int:
            self.total=[]
            self.dfs("", root)
            return(sum(self.total))

    아,, 근데 뭔가 맘에 안든다..,, 뭔가,, 뭐랄까,, 맘에 안든다, 뭔가 더 코드를 정리할 수 있을 것 같은데 

    total =[] 선언도 필요없고 return값으로 바로 받고싶은데에..

     

    아래처럼 변경하긴 했다. 근데 효율 결과 보니까 큰 차이는 없다 흠..

     

     

    저작자표시 (새창열림)

    '2020년 > 코테' 카테고리의 다른 글

    [코테 연습] Largest Perimeter Triangle  (0) 2020.09.09
    [코테 연습] Relative Sort Array  (0) 2020.09.09
    [코테 연습] Largest Time for Given Digits Python  (0) 2020.09.02
    [코테 연습] Delete Node in a BST Python  (0) 2020.09.01
    [코테 연습] Pancake Sorting  (0) 2020.08.31

    잠깐만요~! 읽으신김에 이런 글들은 어떠세요? 👀

    • [코테 연습] Largest Perimeter Triangle 2020.09.09
    • [코테 연습] Relative Sort Array 2020.09.09
    • [코테 연습] Largest Time for Given Digits Python 2020.09.02
    • [코테 연습] Delete Node in a BST Python 2020.09.01
    맨 위로
전체 글 보기
Tistory 로그인
Tistory 로그아웃
로그아웃 글쓰기 관리

열정! 열정! 열정! 🔥

Designed by Nana
블로그 이미지
위지원
데이터 엔지니어로 근무 중에 있으며 데이터와 관련된 일을 모두 좋아합니다!. 특히 ETL 부분에 관심이 가장 크며 데이터를 빛이나게 가공하는 일을 좋아한답니다 ✨

티스토리툴바

티스토리툴바