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
- 뒤집은 소수
- 두 배열 합치기
- 가장 짧은 문자거리
- 배열
- 코테준비
- 10991
- 등수구하기
- java
- 알고리즘
- 누적 계산
- 아스키코드
- 자바
- array
- GitHub #Commit #BaekJoon
- 큰 수 출력하기
- 점수계산
- 10992
- Pointer
- 보이는 학생
- 공통원소 구하기
- 임시반장 정하기
- 연속부분수열
- Two Pointer
- 투 포인터
- 백준
- 격자판
- 인프런
- ArrayList
- 최대 길이
- 모든행과열대각선의합
Archives
- Today
- Total
ezhoon
[인프런] 07_07 이진트리 레벨탐색 본문
📖 문제
- 레벨 탐색 순회 출력
1
2 3
4 5 6 7


⚠️ 주의사항
- BFS의 풀이 방법에 대해 이해 할 것
✍️ 이해
/**
* 1. 새로운 Node 작성
* 2. Queue 생성 tree의 root 값 offer
* 3. Queue의 값이 없을 때까지 반복
* 4. queue 값을 cur 이라는 변수에 저장
* 5. cur의 lt 값 rt값 비교 후 null이 아니면 offer
* 6. L++
*/
✏️ 풀이
import java.util.LinkedList;
import java.util.Queue;
class BFS_Node {
int data;
BFS_Node lt, rt; // 객체 주소 저장
public BFS_Node(int val) {
data = val;
lt = rt = null;
}
}
public class Main {
BFS_Node root;
public void BFS(BFS_Node root) {
Queue<BFS_Node> queue = new LinkedList<>();
queue.offer(root);
int L = 0;
while (!queue.isEmpty()){
int len = queue.size();
for (int i = 0; i < len; i++) {
BFS_Node cur = queue.poll(); // 메인이되는 숫자 집어넣기
System.out.print(cur.data + " ");
if (cur.lt != null) queue.offer(cur.lt); // cur의 왼쪽
if (cur.rt != null) queue.offer(cur.rt); // cur의 오른쪽
}
L++;
System.out.println();
}
}
public static void main(String[] args) {
Main tree = new Main();
tree.root = new BFS_Node(1);
tree.root.lt = new BFS_Node(2);
tree.root.rt = new BFS_Node(3);
tree.root.lt.lt = new BFS_Node(4);
tree.root.lt.rt = new BFS_Node(5);
tree.root.rt.lt = new BFS_Node(6);
tree.root.rt.rt = new BFS_Node(7);
tree.BFS(tree.root);
}
}
'[Java] 인프런 문제풀이 > Recursive, Tree, Graph(DFS, BFS 기초)' 카테고리의 다른 글
[인프런] 07_09 ~ 10 Tree 말단노드까지의 가장 짧은 경로 (DFS, BFS) (0) | 2022.02.16 |
---|---|
[인프런] 07_08 송아지 찾기 (BFS : 상대트리탐색) (0) | 2022.02.15 |
[인프런] 07_06 부분집합 구하기(DFS) (0) | 2022.02.13 |
[인프런] 07_05 이진트리순회 (0) | 2022.02.12 |
[인프런] 07_04 피보나치 재귀(메모이제이션) (0) | 2022.02.12 |
Comments