이진 트리 순회의 방식은 다음과 같이 3가지 방식이 있다.
부모노드를 언제 방문하는지에 따라서 전위순회, 중위순회, 후위순회가 정해짐.

import java.util.*;
public class Main {
static class Node {
int data;
Node lt, rt;
Node(int value) {
this.data = value;
lt = rt = null;
}
}
static Node root;
public static void main(String[] args) {
// depth 0
root = new Node(1);
// depth 1
root.lt = new Node(2);
root.rt = new Node(3);
// depth 2
root.lt.lt = new Node(4);
root.lt.rt = new Node(5);
root.rt.lt = new Node(6);
root.rt.rt = new Node(7);
dfs(root);
}
// 전위순회
public static void dfs(Node root) {
if (root == null) return;
else {
dfs(root.lt);
System.out.println(root.data);
dfs(root.rt);
}
}
}
예제문제
백트래킹(Backtracking) 이란 ?