Skip to content

Commit

Permalink
Section7-5. 이진트리 순회(DFS)
Browse files Browse the repository at this point in the history
  • Loading branch information
gyuseon25 committed Jan 27, 2025
1 parent 083a073 commit e331815
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions Section07/이진트리순회_DFS.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package 인프런.Section07;

import java.security.spec.RSAOtherPrimeInfo;

class Node {
int data;
Node lt, rt;
public Node(int val) {
data = val;
lt=rt=null;
}
}

public class 이진트리순회_DFS {
Node root;

public void DFS(Node root) {
if(root == null) return;
else {
//System.out.print(root.data + " "); //전위 순회
DFS(root.lt);
//System.out.print(root.data + " "); //중위 순회
DFS(root.rt);
//System.out.print(root.data + " "); //후위 순회
}
}

public static void main(String[] args) {
이진트리순회_DFS tree = new 이진트리순회_DFS();

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);
tree.root.rt.lt = new Node(6);
tree.root.rt.rt = new Node(7);

tree.DFS(tree.root);

}
}

0 comments on commit e331815

Please sign in to comment.