Skip to content

문제 027 : 이진 탐색 트리 구현 #96

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: solutions
Choose a base branch
from

Conversation

tiaz0128
Copy link
Contributor

@tiaz0128 tiaz0128 commented Feb 7, 2024

소요시간

  • 30분

사용한 자료구조, 알고리즘

  • 투 포인터
  • 트리

해당 자료구조, 알고리즘을 사용한 근거

  • 이진 트리 탐색

어려웠던 구현 포인트

  • 생각보다 단순하게 구현 가능했다
  • 이전 문제에서 구현해본게 도움이 됐다

구현한 코드의 시간 복잡도

  • $O(N^2)$

추가한 테스트 케이스와 그 이유

  • 다른 조건으로 넣고 맞는지 확인 해봤다.

개선이 필요한 부분은?

  • 트리 문제는 일단 트리를 만드는게 제일 중요하다?
  • 그리고 어떤식으로 트리를 만들지? 잘 작성해야하고
  • 마지막으로 그걸로 아마도? 탐색이 주요한 문제이지 않을까?

Copy link
Contributor

github-actions bot commented Feb 7, 2024

👋 @tiaz0128 님 안녕하세요!
코딩 테스트 합격자 되기(파이썬 편) : 문제 027 를 풀고 있으시네요!
해당 문제의 책 페이지와 프로그래머스 링크를 알려드릴께요!

09장 트리
이진 탐색 트리 구현 ⭐⭐⭐
코딩 테스트 합격자 되기(파이썬 편) - p292
저자 출제

  1. 테스트가 실패한 경우 다시 한번 문제를 풀어서 push 해보세요!
  2. 로컬에서 디버깅도 해보고 스스로 코멘트를 달면서 공부해보세요!
  3. 다시 한번 문제를 풀어서 push 해보세요!

@github-actions github-actions bot added 문제027 Pass 테스트에 성공했습니다. Merge 해주세요! labels Feb 7, 2024
Copy link
Contributor

github-actions bot commented Feb 7, 2024

🎉 @tiaz0128 님. 축하 합니다!

문제 027 테스트를 통과하셨습니다!
solutons 브랜치에 Merge 해주세요!

도움이 필요한 사람들이 있으면 도와주세요! 소통하면서 더 성장 할 수 있는 좋은 기회입니다!
문제 027 - 도움주러 가기

Copy link
Contributor

github-actions bot commented Feb 7, 2024

✨ 아래의 코드는 테스트를 통과한 코드입니다.

class Node:

    def __init__(self, val, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right


class BinarySearchTree:

    def make_tree(self, lst):
        root = Node(lst[0])
        length = len(lst)

        node = root
        for val in lst[1:]:
            while True:
                if val < node.val:
                    if not node.left:
                        node.left = Node(val)
                        break
                    node = node.left
                else:
                    if not node.right:
                        node.right = Node(val)
                        break
                    node = node.right

        return root

    def search_node(self, node, search_value):
        if not node:
            return False

        if node.val == search_value:
            return True

        if node.val > search_value:
            return self.search_node(node.left, search_value)
        else:
            return self.search_node(node.right, search_value)


def solution(lst, search_lst):

    bst = BinarySearchTree()
    root = bst.make_tree(lst)

    return [bst.search_node(root, search_value) for search_value in search_lst]

@tiaz0128 tiaz0128 changed the title 문제 027 : 문제 027 : 이진 탐색 트리 구현 Feb 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Pass 테스트에 성공했습니다. Merge 해주세요! 문제027
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant