{"id":34354,"date":"2024-11-01T09:27:13","date_gmt":"2024-11-01T09:27:13","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34354"},"modified":"2024-11-01T10:57:34","modified_gmt":"2024-11-01T10:57:34","slug":"c-coding-test-course-traversing-trees","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34354\/","title":{"rendered":"C++ Coding Test Course, Traversing Trees"},"content":{"rendered":"<p><body><\/p>\n<h2>Problem Description<\/h2>\n<p>This problem involves implementing a tree traversal algorithm to output the values of the nodes that make up a binary tree in a specific order. A binary tree is a data structure where each node can have up to two child nodes, and there are various ways to visit the nodes. The most common tree traversal methods are preorder traversal, inorder traversal, and postorder traversal.<\/p>\n<h3>Definition of a Tree<\/h3>\n<pre>\n    struct TreeNode {\n        int val; \/\/ Value of the node\n        TreeNode *left; \/\/ Left child node\n        TreeNode *right; \/\/ Right child node\n        TreeNode(int x) : val(x), left(NULL), right(NULL) {} \/\/ Constructor\n    };\n<\/pre>\n<h2>Approach<\/h2>\n<p>The approaches for each traversal are as follows:<\/p>\n<ul>\n<li><b>Preorder Traversal:<\/b> Visit the current node, then visit the left child node, and finally visit the right child node.<\/li>\n<li><b>Inorder Traversal:<\/b> Visit the left child node, then visit the current node, and finally visit the right child node.<\/li>\n<li><b>Postorder Traversal:<\/b> Visit the left child node, then the right child node, and lastly visit the current node.<\/li>\n<\/ul>\n<h3>Preorder Traversal Code Implementation<\/h3>\n<p>Below is the code implemented in C++ for preorder traversal:<\/p>\n<pre><code> \n#include &lt;iostream&gt;\n#include &lt;vector&gt;\n\nusing namespace std;\n\nstruct TreeNode {\n    int val;\n    TreeNode *left;\n    TreeNode *right;\n    TreeNode(int x) : val(x), left(NULL), right(NULL) {}\n};\n\nvoid preorderTraversal(TreeNode* root, vector<int>&amp; result) {\n    if (root == nullptr) return; \/\/ Base case\n    result.push_back(root-&gt;val); \/\/ Visit node\n    preorderTraversal(root-&gt;left, result); \/\/ Visit left subtree\n    preorderTraversal(root-&gt;right, result); \/\/ Visit right subtree\n}\n\nint main() {\n    \/\/ Initialize binary tree\n    TreeNode* root = new TreeNode(1);\n    root-&gt;left = new TreeNode(2);\n    root-&gt;right = new TreeNode(3);\n    root-&gt;left-&gt;left = new TreeNode(4);\n    root-&gt;left-&gt;right = new TreeNode(5);\n    \n    vector<int> result;\n    preorderTraversal(root, result);\n    \n    \/\/ Print results\n    for (int val : result) {\n        cout &lt;&lt; val &lt;&lt; \" \";\n    }\n    return 0;\n}\n    <\/int><\/int><\/code><\/pre>\n<h3>Inorder Traversal Code Implementation<\/h3>\n<p>Below is the code implemented in C++ for inorder traversal:<\/p>\n<pre><code>\nvoid inorderTraversal(TreeNode* root, vector<int>&amp; result) {\n    if (root == nullptr) return; \/\/ Base case\n    inorderTraversal(root-&gt;left, result); \/\/ Visit left subtree\n    result.push_back(root-&gt;val); \/\/ Visit node\n    inorderTraversal(root-&gt;right, result); \/\/ Visit right subtree\n}\n\n\/\/ The main function remains the same\n    <\/int><\/code><\/pre>\n<h3>Postorder Traversal Code Implementation<\/h3>\n<p>Below is the code implemented in C++ for postorder traversal:<\/p>\n<pre><code>\nvoid postorderTraversal(TreeNode* root, vector<int>&amp; result) {\n    if (root == nullptr) return; \/\/ Base case\n    postorderTraversal(root-&gt;left, result); \/\/ Visit left subtree\n    postorderTraversal(root-&gt;right, result); \/\/ Visit right subtree\n    result.push_back(root-&gt;val); \/\/ Visit node\n}\n\n\/\/ The main function remains the same\n    <\/int><\/code><\/pre>\n<h2>Time Complexity and Optimization<\/h2>\n<p>The time complexity of each tree traversal method is O(n). This is because each node in the tree is visited once. The space complexity can be O(h) in the worst case (where h is the height of the tree) if the tree is not complete. If the tree is linear in shape, O(n) space may be needed.<\/p>\n<h3>Optimization Methods<\/h3>\n<p>To further improve the performance of tree traversal, it can be implemented iteratively using a stack. For example, when implementing preorder traversal iteratively, it can be done as follows:<\/p>\n<pre><code>\n#include &lt;stack&gt;\n\nvoid iterativePreorderTraversal(TreeNode* root, vector<int>&amp; result) {\n    if (root == nullptr) return; \n    stack<treenode*> stk;\n    stk.push(root);\n\n    while (!stk.empty()) {\n        TreeNode* node = stk.top();\n        stk.pop();\n        result.push_back(node-&gt;val); \/\/ Visit node\n        if (node-&gt;right != nullptr) stk.push(node-&gt;right); \/\/ Add right child first\n        if (node-&gt;left != nullptr) stk.push(node-&gt;left); \/\/ Add left child\n    }\n}\n\n\/\/ The main function remains the same\n    <\/treenode*><\/int><\/code><\/pre>\n<h2>Conclusion<\/h2>\n<p>In this tutorial, we explained how to implement various traversal algorithms for a binary tree using C++. It is important to understand the methodologies for each traversal and to write specific code to practice this. Binary trees play a crucial role in various fields, so it is advisable to master them.<\/p>\n<h2>References<\/h2>\n<ul>\n<li><a href=\"https:\/\/en.wikipedia.org\/wiki\/Tree_traversal\">Wikipedia: Tree Traversal<\/a><\/li>\n<li><a href=\"https:\/\/www.geeksforgeeks.org\/tree-traversals-inorder-preorder-and-postorder\/\">GeeksforGeeks: Tree Traversals<\/a><\/li>\n<li><a href=\"https:\/\/www.learncpp.com\/cpp-tutorial\/3-2-constructing-and-destroying-a-binary-tree\/\">LearnCpp: Constructing a Binary Tree<\/a><\/li>\n<\/ul>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem Description This problem involves implementing a tree traversal algorithm to output the values of the nodes that make up a binary tree in a specific order. A binary tree is a data structure where each node can have up to two child nodes, and there are various ways to visit the nodes. The most &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34354\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;C++ Coding Test Course, Traversing Trees&#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":[111],"tags":[],"class_list":["post-34354","post","type-post","status-publish","format-standard","hentry","category-c-coding-test-tutorials-2"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>C++ Coding Test Course, Traversing Trees - \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\/34354\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C++ Coding Test Course, Traversing Trees - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Problem Description This problem involves implementing a tree traversal algorithm to output the values of the nodes that make up a binary tree in a specific order. A binary tree is a data structure where each node can have up to two child nodes, and there are various ways to visit the nodes. The most &hellip; \ub354 \ubcf4\uae30 &quot;C++ Coding Test Course, Traversing Trees&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34354\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:27:13+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T10:57:34+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\/34354\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34354\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"C++ Coding Test Course, Traversing Trees\",\"datePublished\":\"2024-11-01T09:27:13+00:00\",\"dateModified\":\"2024-11-01T10:57:34+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34354\/\"},\"wordCount\":343,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"C++ Coding Test Tutorials\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34354\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34354\/\",\"name\":\"C++ Coding Test Course, Traversing Trees - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:27:13+00:00\",\"dateModified\":\"2024-11-01T10:57:34+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34354\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34354\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34354\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++ Coding Test Course, Traversing Trees\"}]},{\"@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":"C++ Coding Test Course, Traversing Trees - \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\/34354\/","og_locale":"ko_KR","og_type":"article","og_title":"C++ Coding Test Course, Traversing Trees - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Problem Description This problem involves implementing a tree traversal algorithm to output the values of the nodes that make up a binary tree in a specific order. A binary tree is a data structure where each node can have up to two child nodes, and there are various ways to visit the nodes. The most &hellip; \ub354 \ubcf4\uae30 \"C++ Coding Test Course, Traversing Trees\"","og_url":"https:\/\/atmokpo.com\/w\/34354\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:27:13+00:00","article_modified_time":"2024-11-01T10:57:34+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\/34354\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34354\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"C++ Coding Test Course, Traversing Trees","datePublished":"2024-11-01T09:27:13+00:00","dateModified":"2024-11-01T10:57:34+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34354\/"},"wordCount":343,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["C++ Coding Test Tutorials"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34354\/","url":"https:\/\/atmokpo.com\/w\/34354\/","name":"C++ Coding Test Course, Traversing Trees - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:27:13+00:00","dateModified":"2024-11-01T10:57:34+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34354\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34354\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34354\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"C++ Coding Test Course, Traversing Trees"}]},{"@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\/34354","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=34354"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34354\/revisions"}],"predecessor-version":[{"id":34355,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34354\/revisions\/34355"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34354"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34354"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34354"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}