From 1d4a94df4ec2ae3ca09788b47c7635b7459904d2 Mon Sep 17 00:00:00 2001 From: gyuseon Date: Thu, 30 Jan 2025 18:21:14 +0900 Subject: [PATCH] =?UTF-8?q?Section7-9.=20Tree=20=EB=A7=90=EB=8B=A8?= =?UTF-8?q?=EB=85=B8=EB=93=9C=EA=B9=8C=EC=A7=80=EC=9D=98=20=EA=B0=80?= =?UTF-8?q?=EC=9E=A5=20=EC=A7=A7=EC=9D=80=20=EA=B2=BD=EB=A1=9C(BFS)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\213\250\352\261\260\353\246\254_BFS.java" | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 "Section07/\353\247\220\353\213\250\353\205\270\353\223\234_\354\265\234\353\213\250\352\261\260\353\246\254_BFS.java" diff --git "a/Section07/\353\247\220\353\213\250\353\205\270\353\223\234_\354\265\234\353\213\250\352\261\260\353\246\254_BFS.java" "b/Section07/\353\247\220\353\213\250\353\205\270\353\223\234_\354\265\234\353\213\250\352\261\260\353\246\254_BFS.java" new file mode 100644 index 0000000..4a1c6c9 --- /dev/null +++ "b/Section07/\353\247\220\353\213\250\353\205\270\353\223\234_\354\265\234\353\213\250\352\261\260\353\246\254_BFS.java" @@ -0,0 +1,43 @@ +package 인프런.Section07; + +import java.util.LinkedList; +import java.util.Queue; + +public class 말단노드_최단거리_BFS { + Node root; + + public int BFS(Node root) { + Queue queue = new LinkedList<>(); + + int L = 0; + queue.offer(root); + + while(!queue.isEmpty()) { + int len = queue.size(); + for(int i = 0; i < len; i++) { + Node tmp = queue.poll(); + if(tmp.lt == null && tmp.rt == null) return L; + if (tmp.lt != null) { + queue.offer(tmp.lt); + } + if (tmp.rt != null) { + queue.offer(tmp.rt); + } + } + L++; + } + return 0; + } + + public static void main(String[] args) { + 말단노드_최단거리_BFS tree = new 말단노드_최단거리_BFS(); + + tree.root = new Node(1); + tree.root.lt = new Node(2); + tree.root.rt = new Node(3); + tree.root.lt.lt = new Node(4); + tree.root.lt.rt = new Node(5); + + System.out.print(tree.BFS(tree.root)); + } +}