Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- C언어
- lob
- 백준
- BOJ Python
- HTML
- 풀이
- c++
- Python
- 생활코딩
- The Loard of BOF
- 드림핵
- hackctf
- PHP 웹페이지 만들기
- 기계학습
- WarGame
- siss
- SWEA
- hackerrank
- Sookmyung Information Security Study
- Javascript
- CSS
- 숙명여자대학교 정보보안동아리
- 자료구조 복습
- c
- 머신러닝
- 웹페이지 만들기
- XSS Game
- 숙명여자대학교 정보보안 동아리
- 파이썬
- BOJ
Archives
- Today
- Total
혜랑's STORY
[HackerRank_C] Tree: Height of a Binary Tree 본문
2021년도 1학기 6주차 과제(자료구조 복습용 tree)
문제
- 링크 : https://www.hackerrank.com/challenges/tree-height-of-a-binary-tree/problem
- 문제 요약 : 트리의 height를 구하여라. (이 문제에서는 edge 수를 height라고 한다.)


풀이
int getHeight(struct node* root) {
// Write your code here
if(!root)
return -1;
else{
int left_h = getHeight(root->left);
int right_h = getHeight(root->right);
return 1 + (left_h > right_h ? left_h : right_h);
}
}
- 만약 root가 비어있다면, 노드의 끝이 전 노드라는 것이므로 방금 지나온 edge는 없다. 따라서 -1을 return 한다.
- 재귀호출을 통해 LS와 RS의 높이를 구하고 더 큰 값에 1을 더한 뒤 return 한다.
결과

'2021 SISS 21기 활동 > 1학기 C' 카테고리의 다른 글
[HackerRank_C] Recursive : Recursive Digit Sum (0) | 2021.06.27 |
---|---|
[HackerRank_C] Search : Ice Cream Parlor (0) | 2021.06.24 |
[HackerRank_C] Tree: Inorder, Postorder Traversal (0) | 2021.05.11 |
[HackerRank_C] Quicksort1 - partition (0) | 2021.05.06 |
[HackerRank_C] Sherlock and Array (0) | 2021.04.11 |