개요
Tree 문제 모음
- pre-order 구현 - Tree: Preorder Traversal
- post-order 구현 - Tree: Postorder Traversal
- in-order 구현 - Tree: Inorder Traversal
- tree의 높이 구하기 - Tree: Height of a Binary Tree
이번에는 Tree 자료구조에 대한 문제를 풀어보았다.
tree의 특징
Tree
란 아래의 그림처럼 binary tree가 유명하며, 아래와 같이 root 밑에 left, right가지로 구성되어 있는 형태이다.
데이터를 중복 없이 저장하고, 데이터 검색 시 O(logN)1 이라는 빠른 장점이 있다. 단, 아래의 그림처럼 left < root < right를 만족할 때 자료구조의 검색이 용이해진다.
tree의 탐색 방법
tree를 탐색하는 여러가지 방법이 있는데, 1~3 번 문제가 바로 그 종류이다.
탐색하는 방법은 pre, post, in 이 있으며 아래의 그림과 같은 순서를 만족한다.
각각의 탐색방법을 구현할 때는 다음에 소개할 재귀 함수 형태로 구현하는 것이 쉽다.
pre-order 탐색 방법 구현
pre-order를 구현한 코드이다. root -> left -> right 순서대로 호출 한다.
pre-order.py | |
---|---|
post-order 탐색 방법 구현
post-order를 구현한 코드이다. left -> right -> root 순서대로 호출 한다.
post-order.py | |
---|---|
in-order 탐색 방법 구현
in-order를 구현한 코드이다. left -> root -> right 순서대로 호출 한다.
in-order.py | |
---|---|
tree의 높이 구하기
tree의 높이를 구하려고한다. 아래의 그림처럼 tree의 잎사귀 Node2 노드의 최대 level이 tree의 높이라고 말한다.
탐색 할 case 작성
tree를 높이를 구할 아래의 case를 나누어서 생각해야 할 것이다.
4번
같은 경우 잎사귀 Node가 없으므로 높이는 0이다. 2번
,3번
같은 경우 각각 존재하므로 높이는 적어도 1이 추가되어야 한다.
문제는 1번
인데 left 또는 right가 어디까지 뻗어있는지 모르는 상황이므로 적어도 높이는 1개이고, 두 Node의 최대 값
을 구하면 된다.
이를 구현한 코드는 아래와 같다.
get-height.py | |
---|---|