{"id":35170,"date":"2024-11-01T09:36:23","date_gmt":"2024-11-01T09:36:23","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=35170"},"modified":"2024-11-01T09:36:55","modified_gmt":"2024-11-01T09:36:55","slug":"%ec%bd%94%ed%8b%80%eb%a6%b0-%ec%bd%94%eb%94%a9%ed%85%8c%ec%8a%a4%ed%8a%b8-%ea%b0%95%ec%a2%8c-tree-traversal","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/35170\/","title":{"rendered":"Tree Traversal"},"content":{"rendered":"<p><body><\/p>\n<p>The topic of today is Tree Traversal. A tree is a type of data structure that is hierarchically organized with elements that have specific relationships. Understanding trees, one of the commonly used data structures, and implementing basic traversal algorithms is very useful in coding tests. In this article, we will start with the basic concepts of trees, explore various traversal methods, and discuss the implementation in Kotlin.<\/p>\n<h2>1. Basic Concepts of Trees<\/h2>\n<p>A tree is a data structure composed of nodes. Each node can have values and child nodes. A tree can be described using the following key terms:<\/p>\n<ul>\n<li><strong>Root Node<\/strong>: The topmost node of the tree.<\/li>\n<li><strong>Leaf Node<\/strong>: A node that has no child nodes.<\/li>\n<li><strong>Internal Node<\/strong>: A node that has child nodes.<\/li>\n<li><strong>Subtree<\/strong>: A tree with a specific node as its root.<\/li>\n<\/ul>\n<h2>2. Types of Tree Traversal<\/h2>\n<p>Tree traversal methods can generally be divided into three types:<\/p>\n<ol>\n<li><strong>Pre-order Traversal<\/strong>: Visit the node &#8211; traverse the left subtree &#8211; traverse the right subtree<\/li>\n<li><strong>In-order Traversal<\/strong>: Traverse the left subtree &#8211; visit the node &#8211; traverse the right subtree<\/li>\n<li><strong>Post-order Traversal<\/strong>: Traverse the left subtree &#8211; traverse the right subtree &#8211; visit the node<\/li>\n<\/ol>\n<h2>3. Problem Description<\/h2>\n<p>Let&#8217;s assume we are given a binary tree as follows:<\/p>\n<pre>\n                 1\n               \/   \\\n              2     3\n             \/ \\   \/ \\\n            4   5 6   7\n    <\/pre>\n<p>Please list the results of pre-order, in-order, and post-order traversals for this tree.<\/p>\n<h2>4. Problem Solving Process<\/h2>\n<p>The first requirement to solve this problem is to define a class structure for the tree. In Kotlin, we can implement the nodes of a binary tree in the following way:<\/p>\n<h3>4.1) Define the Binary Tree Node Class<\/h3>\n<pre><code>class TreeNode(val value: Int) {\n        var left: TreeNode? = null\n        var right: TreeNode? = null\n    }<\/code><\/pre>\n<p>The above class has a <code>value<\/code> that stores the node&#8217;s value and variables <code>left<\/code> and <code>right<\/code> that reference the left and right child nodes.<\/p>\n<h3>4.2) Implementing Pre-order Traversal<\/h3>\n<p>Pre-order traversal visits the node before visiting its children. The function for this is as follows:<\/p>\n<pre><code>fun preOrderTraversal(node: TreeNode?) {\n        if (node == null) return\n        print(\"${node.value} \")\n        preOrderTraversal(node.left)\n        preOrderTraversal(node.right)\n    }<\/code><\/pre>\n<h3>4.3) Implementing In-order Traversal<\/h3>\n<p>In-order traversal first visits the left child and then the current node. It can be implemented as follows:<\/p>\n<pre><code>fun inOrderTraversal(node: TreeNode?) {\n        if (node == null) return\n        inOrderTraversal(node.left)\n        print(\"${node.value} \")\n        inOrderTraversal(node.right)\n    }<\/code><\/pre>\n<h3>4.4) Implementing Post-order Traversal<\/h3>\n<p>Post-order traversal visits both children before visiting the current node. The implementation is as follows:<\/p>\n<pre><code>fun postOrderTraversal(node: TreeNode?) {\n        if (node == null) return\n        postOrderTraversal(node.left)\n        postOrderTraversal(node.right)\n        print(\"${node.value} \")\n    }<\/code><\/pre>\n<h2>5. Creating and Testing the Tree<\/h2>\n<p>Now let&#8217;s create the tree and test each traversal method. You can structure and execute the tree traversal with the following code:<\/p>\n<pre><code>fun main() {\n        val root = TreeNode(1)\n        root.left = TreeNode(2)\n        root.right = TreeNode(3)\n        root.left?.left = TreeNode(4)\n        root.left?.right = TreeNode(5)\n        root.right?.left = TreeNode(6)\n        root.right?.right = TreeNode(7)\n\n        print(\"Pre-order Traversal: \")\n        preOrderTraversal(root)\n        println()\n\n        print(\"In-order Traversal: \")\n        inOrderTraversal(root)\n        println()\n\n        print(\"Post-order Traversal: \")\n        postOrderTraversal(root)\n        println()\n    }<\/code><\/pre>\n<h2>6. Code Execution Results<\/h2>\n<p>When executing the above program, you can obtain the following results:<\/p>\n<pre><code>Pre-order Traversal: 1 2 4 5 3 6 7 \nIn-order Traversal: 4 2 5 1 6 3 7 \nPost-order Traversal: 4 5 2 6 7 3 1 \n<\/code><\/pre>\n<h2>7. Summary<\/h2>\n<p>In this article, we learned the basic concepts of trees and the three methods of tree traversal: pre-order, in-order, and post-order. We implemented each traversal method in Kotlin and could understand the tree structure through a simple example that can be used in real situations. Since tree problems are frequently encountered in coding tests, it is important to practice sufficiently to become familiar with them.<\/p>\n<h2>8. Additional Practice Problems<\/h2>\n<p>Enhance your understanding of tree traversal by solving the following problems:<\/p>\n<ol>\n<li>Write a function to calculate the depth of a given binary tree.<\/li>\n<li>Write a function to find the maximum path sum in a binary tree.<\/li>\n<\/ol>\n<h2>9. Conclusion<\/h2>\n<p>Now that you have a basic understanding of tree structures and traversal methods, you are prepared to challenge yourself with more complex coding problems. As you solve various algorithm problems involving trees, continue to build your skills in this area. In the next lesson, we will cover graph search. Thank you!<\/p>\n<footer>\n<p>Copyright \u00a9 2023 &#8211; Coding Test Course<\/p>\n<\/footer>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The topic of today is Tree Traversal. A tree is a type of data structure that is hierarchically organized with elements that have specific relationships. Understanding trees, one of the commonly used data structures, and implementing basic traversal algorithms is very useful in coding tests. In this article, we will start with the basic concepts &hellip; <a href=\"https:\/\/atmokpo.com\/w\/35170\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Tree Traversal&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[106],"tags":[],"class_list":["post-35170","post","type-post","status-publish","format-standard","hentry","category----en"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Tree Traversal - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/atmokpo.com\/w\/35170\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Tree Traversal - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"The topic of today is Tree Traversal. A tree is a type of data structure that is hierarchically organized with elements that have specific relationships. Understanding trees, one of the commonly used data structures, and implementing basic traversal algorithms is very useful in coding tests. In this article, we will start with the basic concepts &hellip; \ub354 \ubcf4\uae30 &quot;Tree Traversal&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/35170\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:36:23+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T09:36:55+00:00\" \/>\n<meta name=\"author\" content=\"root\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:site\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"root\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"3\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/35170\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/35170\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Tree Traversal\",\"datePublished\":\"2024-11-01T09:36:23+00:00\",\"dateModified\":\"2024-11-01T09:36:55+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/35170\/\"},\"wordCount\":544,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Kotlin coding test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/35170\/\",\"url\":\"https:\/\/atmokpo.com\/w\/35170\/\",\"name\":\"Tree Traversal - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:36:23+00:00\",\"dateModified\":\"2024-11-01T09:36:55+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/35170\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/35170\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/35170\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Tree Traversal\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/atmokpo.com\/w\/#website\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/atmokpo.com\/w\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"contentUrl\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"width\":400,\"height\":400,\"caption\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\"},\"image\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/bebubo4\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\",\"name\":\"root\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"caption\":\"root\"},\"sameAs\":[\"http:\/\/atmokpo.com\/w\"],\"url\":\"https:\/\/atmokpo.com\/w\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Tree Traversal - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/atmokpo.com\/w\/35170\/","og_locale":"ko_KR","og_type":"article","og_title":"Tree Traversal - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"The topic of today is Tree Traversal. A tree is a type of data structure that is hierarchically organized with elements that have specific relationships. Understanding trees, one of the commonly used data structures, and implementing basic traversal algorithms is very useful in coding tests. In this article, we will start with the basic concepts &hellip; \ub354 \ubcf4\uae30 \"Tree Traversal\"","og_url":"https:\/\/atmokpo.com\/w\/35170\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:36:23+00:00","article_modified_time":"2024-11-01T09:36:55+00:00","author":"root","twitter_card":"summary_large_image","twitter_creator":"@bebubo4","twitter_site":"@bebubo4","twitter_misc":{"\uae00\uc4f4\uc774":"root","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"3\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/35170\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/35170\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Tree Traversal","datePublished":"2024-11-01T09:36:23+00:00","dateModified":"2024-11-01T09:36:55+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/35170\/"},"wordCount":544,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Kotlin coding test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/35170\/","url":"https:\/\/atmokpo.com\/w\/35170\/","name":"Tree Traversal - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:36:23+00:00","dateModified":"2024-11-01T09:36:55+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/35170\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/35170\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/35170\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Tree Traversal"}]},{"@type":"WebSite","@id":"https:\/\/atmokpo.com\/w\/#website","url":"https:\/\/atmokpo.com\/w\/","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","description":"","publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/atmokpo.com\/w\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/atmokpo.com\/w\/#organization","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","url":"https:\/\/atmokpo.com\/w\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/","url":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","contentUrl":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","width":400,"height":400,"caption":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8"},"image":{"@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/bebubo4"]},{"@type":"Person","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7","name":"root","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","caption":"root"},"sameAs":["http:\/\/atmokpo.com\/w"],"url":"https:\/\/atmokpo.com\/w\/author\/root\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/35170","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/comments?post=35170"}],"version-history":[{"count":2,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/35170\/revisions"}],"predecessor-version":[{"id":35209,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/35170\/revisions\/35209"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=35170"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=35170"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=35170"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}