이진트리순회(DFS : Depth-First Search)
이진트리순회(DFS : Depth-First Search)
import java.util.*; class Node{ int data; Node lt, rt; public Node(int val) { data=val; lt=rt=null; } } public class Main{ Node root; public void DFS(Node root){ if(root==null) return; else{ DFS(root.lt); DFS(root.rt); System.out.print(root.data+" "); // 위치에 따라 전위 중위 후위 결정 } } public static void main(String args[]) { Main tree=new Main(); 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); } }
손으로 그려보면서 해보니 훨씬 이해가 쉬웠다 !
Author And Source
이 문제에 관하여(이진트리순회(DFS : Depth-First Search)), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@_jackson/이진트리순회DFS-Depth-First-Search저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)