{"id":33890,"date":"2024-11-01T09:21:41","date_gmt":"2024-11-01T09:21:41","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=33890"},"modified":"2024-11-01T10:55:24","modified_gmt":"2024-11-01T10:55:24","slug":"c-coding-test-course-finding-the-lowest-common-ancestor-2","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/33890\/","title":{"rendered":"C# Coding Test Course, Finding the Lowest Common Ancestor 2"},"content":{"rendered":"<p><body><\/p>\n<article>\n<header>\n<p>Author: [Author Name]<\/p>\n<p>Written on: [Date]<\/p>\n<\/header>\n<section>\n<h2>Introduction<\/h2>\n<p>\n                In this course, we will discuss how to solve the &#8216;Lowest Common Ancestor (LCA)&#8217; problem using the C# programming language.<br \/>\n                The Lowest Common Ancestor refers to the deepest node that is a common ancestor of two nodes in a binary tree.<br \/>\n                We will explain the process of solving this problem in detail using specific algorithms and data structures.\n            <\/p>\n<\/section>\n<section>\n<h2>Problem Description<\/h2>\n<p>\n                The problem is to find the lowest common ancestor of two given nodes A and B in a given binary tree.<br \/>\n                The binary tree is non-empty, and all nodes have unique values.<br \/>\n                Additionally, nodes A and B are assumed to exist in the tree.\n            <\/p>\n<\/section>\n<section>\n<h2>Input Format<\/h2>\n<pre>\n                - First line: Number of nodes in the tree N (1 \u2264 N \u2264 10^5)\n                - Second line: N space-separated integers representing the node values of the tree\n                - Third line: Two integers A, B (A, B exist in the tree)\n            <\/pre>\n<\/section>\n<section>\n<h2>Output Format<\/h2>\n<pre>\n                - The value of the lowest common ancestor of A and B\n            <\/pre>\n<\/section>\n<section>\n<h2>Example Problem<\/h2>\n<pre>\n                Input:\n                7\n                3 5 1 6 2 0 8\n                5 1\n\n                Output:\n                3\n            <\/pre>\n<\/section>\n<section>\n<h2>Solution Method<\/h2>\n<p>\n                There are various methods to find the lowest common ancestor, but it is generally effective to explore the tree<br \/>\n                using Depth First Search (DFS) while considering the depth of the tree.<br \/>\n                In the case of a Binary Search Tree (BST), it is possible to efficiently find the ancestor by determining which<br \/>\n                subtree A and B belong to. However, in a general binary tree, utilizing information from the parent nodes is necessary.\n            <\/p>\n<\/section>\n<section>\n<h2>Algorithm Design<\/h2>\n<p>\n                The main steps of the algorithm are as follows.\n            <\/p>\n<ol>\n<li>Construct the tree given in the input.<\/li>\n<li>Store the parent node for each node in a map or array.<\/li>\n<li>Record the path from node A to the root.<\/li>\n<li>Record the path from node B to the root.<\/li>\n<li>Find the first common node in the two paths.<\/li>\n<\/ol>\n<\/section>\n<section>\n<h2>C# Code Implementation<\/h2>\n<p>\n                Below is the C# code implementing the above algorithm.\n            <\/p>\n<pre>\n                <code>\nusing System;\nusing System.Collections.Generic;\n\nclass TreeNode {\n    public int Value;\n    public TreeNode Left, Right;\n    \n    public TreeNode(int value) {\n        this.Value = value;\n        this.Left = null;\n        this.Right = null;\n    }\n}\n\nclass Program {\n    static Dictionary<int, TreeNode> nodeMap = new Dictionary<int, TreeNode>();\n    static TreeNode root;\n\n    public static void Main(string[] args) {\n        int N = Convert.ToInt32(Console.ReadLine());\n        int[] values = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);\n        int A = Convert.ToInt32(Console.ReadLine());\n        int B = Convert.ToInt32(Console.ReadLine());\n\n        BuildTree(values, 0, N);\n        TreeNode ancestor = FindLCA(root, A, B);\n        Console.WriteLine(ancestor.Value);\n    }\n\n    static TreeNode BuildTree(int[] values, int index, int N) {\n        if (index < N) {\n            TreeNode node = new TreeNode(values[index]);\n            nodeMap[values[index]] = node;\n            node.Left = BuildTree(values, 2 * index + 1, N);\n            node.Right = BuildTree(values, 2 * index + 2, N);\n            return node;\n        }\n        return null;\n    }\n\n    static TreeNode FindLCA(TreeNode node, int A, int B) {\n        if (node == null) return null;\n        if (node.Value == A || node.Value == B) return node;\n\n        TreeNode leftLCA = FindLCA(node.Left, A, B);\n        TreeNode rightLCA = FindLCA(node.Right, A, B);\n\n        if (leftLCA != null &#038;&#038; rightLCA != null) return node;\n        return (leftLCA != null) ? leftLCA : rightLCA;\n    }\n}\n                <\/code>\n            <\/pre>\n<\/section>\n<section>\n<h2>Code Explanation<\/h2>\n<p>\n                1. <strong>TreeNode Class<\/strong>: Represents a node in a binary tree. Each node has a value and left and right children.\n            <\/p>\n<p>\n                2. <strong>Main Method<\/strong>: The starting point of the program. It receives input from the user and builds the tree, then finds the LCA.\n            <\/p>\n<p>\n                3. <strong>BuildTree Method<\/strong>: Constructs the binary tree using an array. It is called recursively to build the tree.\n            <\/p>\n<p>\n                4. <strong>FindLCA Method<\/strong>: Recursively traverses the tree to find the lowest common ancestor of the two nodes A and B.\n            <\/p>\n<\/section>\n<section>\n<h2>Time Complexity<\/h2>\n<p>\n                The time complexity of this algorithm is O(N).<br \/>\n                It increases proportionally to the number of nodes since it visits each node once while traversing the tree.<br \/>\n                The space complexity is also O(N), which is due to the space used by the recursive call stack and for storing node information.\n            <\/p>\n<\/section>\n<section>\n<h2>Conclusion<\/h2>\n<p>\n                In this course, we learned how to solve the lowest common ancestor problem using C#.<br \/>\n                By understanding the principles of the algorithm and implementing the code, you would have gained insights on how to effectively tackle similar tree problems in coding tests.<br \/>\n                In the next session, we will delve deeper into other data structures or algorithm problems.<br \/>\n                Thank you.\n            <\/p>\n<\/section>\n<\/article>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Author: [Author Name] Written on: [Date] Introduction In this course, we will discuss how to solve the &#8216;Lowest Common Ancestor (LCA)&#8217; problem using the C# programming language. The Lowest Common Ancestor refers to the deepest node that is a common ancestor of two nodes in a binary tree. We will explain the process of solving &hellip; <a href=\"https:\/\/atmokpo.com\/w\/33890\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;C# Coding Test Course, Finding the Lowest Common Ancestor 2&#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":[90],"tags":[],"class_list":["post-33890","post","type-post","status-publish","format-standard","hentry","category-c-coding-test-tutorials"],"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, Finding the Lowest Common Ancestor 2 - \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\/33890\/\" \/>\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, Finding the Lowest Common Ancestor 2 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Author: [Author Name] Written on: [Date] Introduction In this course, we will discuss how to solve the &#8216;Lowest Common Ancestor (LCA)&#8217; problem using the C# programming language. The Lowest Common Ancestor refers to the deepest node that is a common ancestor of two nodes in a binary tree. We will explain the process of solving &hellip; \ub354 \ubcf4\uae30 &quot;C# Coding Test Course, Finding the Lowest Common Ancestor 2&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/33890\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:21:41+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T10:55:24+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\/33890\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33890\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"C# Coding Test Course, Finding the Lowest Common Ancestor 2\",\"datePublished\":\"2024-11-01T09:21:41+00:00\",\"dateModified\":\"2024-11-01T10:55:24+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33890\/\"},\"wordCount\":460,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"C# Coding Test Tutorials\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/33890\/\",\"url\":\"https:\/\/atmokpo.com\/w\/33890\/\",\"name\":\"C# Coding Test Course, Finding the Lowest Common Ancestor 2 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:21:41+00:00\",\"dateModified\":\"2024-11-01T10:55:24+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33890\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/33890\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/33890\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C# Coding Test Course, Finding the Lowest Common Ancestor 2\"}]},{\"@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, Finding the Lowest Common Ancestor 2 - \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\/33890\/","og_locale":"ko_KR","og_type":"article","og_title":"C# Coding Test Course, Finding the Lowest Common Ancestor 2 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Author: [Author Name] Written on: [Date] Introduction In this course, we will discuss how to solve the &#8216;Lowest Common Ancestor (LCA)&#8217; problem using the C# programming language. The Lowest Common Ancestor refers to the deepest node that is a common ancestor of two nodes in a binary tree. We will explain the process of solving &hellip; \ub354 \ubcf4\uae30 \"C# Coding Test Course, Finding the Lowest Common Ancestor 2\"","og_url":"https:\/\/atmokpo.com\/w\/33890\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:21:41+00:00","article_modified_time":"2024-11-01T10:55:24+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\/33890\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/33890\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"C# Coding Test Course, Finding the Lowest Common Ancestor 2","datePublished":"2024-11-01T09:21:41+00:00","dateModified":"2024-11-01T10:55:24+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/33890\/"},"wordCount":460,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["C# Coding Test Tutorials"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/33890\/","url":"https:\/\/atmokpo.com\/w\/33890\/","name":"C# Coding Test Course, Finding the Lowest Common Ancestor 2 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:21:41+00:00","dateModified":"2024-11-01T10:55:24+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/33890\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/33890\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/33890\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"C# Coding Test Course, Finding the Lowest Common Ancestor 2"}]},{"@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\/33890","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=33890"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33890\/revisions"}],"predecessor-version":[{"id":33891,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33890\/revisions\/33891"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=33890"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=33890"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=33890"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}