20250903 baekjoon
This commit is contained in:
parent
dc7c1fb0b3
commit
7a2c1e04a6
97
code_study/Baekjoon/java/_1991.java
Normal file
97
code_study/Baekjoon/java/_1991.java
Normal file
@ -0,0 +1,97 @@
|
||||
import java.util.*;
|
||||
|
||||
class Node {
|
||||
char value;
|
||||
Node left;
|
||||
Node right;
|
||||
|
||||
public Node(char value) {
|
||||
this.value = value;
|
||||
this.left = null;
|
||||
this.right = null;
|
||||
}
|
||||
}
|
||||
|
||||
class Tree {
|
||||
Node[] data;
|
||||
|
||||
public Tree(int N) {
|
||||
this.data = new Node[N];
|
||||
}
|
||||
|
||||
public void insertNode_byCharlist(char[] char_list) {
|
||||
char root = char_list[0];
|
||||
char left = char_list[1];
|
||||
char right = char_list[2];
|
||||
|
||||
if(data[root-'A'] == null) {
|
||||
data[root-'A'] = new Node(root);
|
||||
}
|
||||
|
||||
if('A'<=left && left<='Z') {
|
||||
if(data[left-'A'] == null) data[left-'A'] = new Node(left);
|
||||
data[root-'A'].left = data[left-'A'];
|
||||
}
|
||||
|
||||
if('A'<=right && right<='Z') {
|
||||
if(data[right-'A'] == null) data[right-'A'] = new Node(right);
|
||||
data[root-'A'].right = data[right-'A'];
|
||||
}
|
||||
}
|
||||
|
||||
public void preorder() {
|
||||
preorder(this.data[0]);
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
public void inorder() {
|
||||
inorder(this.data[0]);
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
public void postorder() {
|
||||
postorder(this.data[0]);
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
private void preorder(Node node) {
|
||||
if (node == null) return;
|
||||
|
||||
System.out.print(node.value);
|
||||
preorder(node.left);
|
||||
preorder(node.right);
|
||||
}
|
||||
|
||||
private void inorder(Node node) {
|
||||
if (node == null) return;
|
||||
|
||||
inorder(node.left);
|
||||
System.out.print(node.value);
|
||||
inorder(node.right);
|
||||
}
|
||||
|
||||
private void postorder(Node node) {
|
||||
if (node == null) return;
|
||||
|
||||
postorder(node.left);
|
||||
postorder(node.right);
|
||||
System.out.print(node.value);
|
||||
}
|
||||
}
|
||||
|
||||
public class _1991 {
|
||||
public static void main(String[] args) {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
int N = Integer.parseInt(sc.nextLine());
|
||||
Tree tree = new Tree(N);
|
||||
for(int i=0; i<N; i++) {
|
||||
char[] input = sc.nextLine().replace(" ", "").toCharArray();
|
||||
tree.insertNode_byCharlist(input);
|
||||
}
|
||||
sc.close();
|
||||
|
||||
tree.preorder();
|
||||
tree.inorder();
|
||||
tree.postorder();
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user