{"id":34618,"date":"2024-11-01T09:30:10","date_gmt":"2024-11-01T09:30:10","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34618"},"modified":"2024-11-01T11:40:28","modified_gmt":"2024-11-01T11:40:28","slug":"javascript-coding-test-course-counting-the-number-of-leaf-nodes","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34618\/","title":{"rendered":"JavaScript Coding Test Course, Counting the Number of Leaf Nodes"},"content":{"rendered":"<article>\n<section>\n<h2>1. Problem Definition<\/h2>\n<p>\n            The problem is to count the number of leaf nodes in a binary tree. A leaf node is a node that has no child nodes. Counting the number of nodes with no children is a good exercise for understanding the structure of the tree and utilizing exploration algorithms. To solve this problem, we can approach it using either recursive or iterative methods.\n        <\/p>\n<\/section>\n<section>\n<h2>2. Problem Example<\/h2>\n<p>\n            Let&#8217;s assume we are given the following binary tree:\n        <\/p>\n<pre>\n             1\n            \/ \\\n           2   3\n          \/ \\\n         4   5\n        <\/pre>\n<p>\n            The leaf nodes of the above tree are 4, 5, and 3, totaling 3. We need to receive a tree like this as input and return the number of leaf nodes.\n        <\/p>\n<\/section>\n<section>\n<h2>3. Algorithm Approach<\/h2>\n<p>\n            There are several ways to determine the number of leaf nodes. The most common method we will use is Depth-First Search (DFS) utilizing recursion. This method involves exploring the tree in a depth-first manner to find leaf nodes and count them.\n        <\/p>\n<\/section>\n<section>\n<h2>4. Algorithm Explanation<\/h2>\n<p>\n            The following is a basic outline of the algorithm to count leaf nodes:\n        <\/p>\n<ol>\n<li>Check if the given node is null. If it is null, return 0.<\/li>\n<li>If the node is a leaf node (i.e., both left and right children are null), return 1.<\/li>\n<li>Recursively call the left and right subtrees to determine the count of leaf nodes in each.<\/li>\n<li>Add the counts of the left and right leaf nodes and return the result.<\/li>\n<\/ol>\n<\/section>\n<section>\n<h2>5. JavaScript Implementation<\/h2>\n<p>\n            Below is the code implementing the above algorithm using JavaScript:\n        <\/p>\n<pre>\n            <code>\n                class TreeNode {\n                    constructor(value) {\n                        this.value = value;\n                        this.left = null;\n                        this.right = null;\n                    }\n                }\n\n                function countLeafNodes(node) {\n                    \/\/ Base case: null node\n                    if (node === null) {\n                        return 0;\n                    }\n                    \/\/ Leaf node condition\n                    if (node.left === null && node.right === null) {\n                        return 1;\n                    }\n                    \/\/ Count leaf nodes with a recursive call\n                    return countLeafNodes(node.left) + countLeafNodes(node.right);\n                }\n\n                \/\/ Example tree construction\n                const root = new TreeNode(1);\n                root.left = new TreeNode(2);\n                root.right = new TreeNode(3);\n                root.left.left = new TreeNode(4);\n                root.left.right = new TreeNode(5);\n\n                console.log(countLeafNodes(root)); \/\/ Expected output: 3\n            <\/code>\n        <\/pre>\n<\/section>\n<section>\n<h2>6. Algorithm Analysis<\/h2>\n<p>\n            The time complexity of this algorithm is O(n) because we need to visit every node in the tree once. Here, n refers to the number of nodes. The space complexity is O(h) in the worst case, where h represents the height of the tree. This is due to the depth of the recursive call stack.\n        <\/p>\n<p>\n            If the tree is empty or if all nodes are skewed to one side, the stack depth may increase. If the tree is balanced, the height would be log(n).\n        <\/p>\n<\/section>\n<section>\n<h2>7. Iterative Method<\/h2>\n<p>\n            We can also implement DFS using an iterative approach. This involves using a stack to track the current node and count nodes with no children. Below is an example of an iterative implementation:\n        <\/p>\n<pre>\n            <code>\n                function countLeafNodesIterative(root) {\n                    if (root === null) {\n                        return 0;\n                    }\n\n                    let stack = [root];\n                    let leafCount = 0;\n\n                    while (stack.length > 0) {\n                        let node = stack.pop();\n\n                        \/\/ Check if the node is a leaf node\n                        if (node.left === null && node.right === null) {\n                            leafCount++;\n                        }\n\n                        \/\/ Add child nodes to the stack\n                        if (node.right !== null) {\n                            stack.push(node.right);\n                        }\n                        if (node.left !== null) {\n                            stack.push(node.left);\n                        }\n                    }\n\n                    return leafCount;\n                }\n\n                console.log(countLeafNodesIterative(root)); \/\/ Expected output: 3\n            <\/code>\n        <\/pre>\n<\/section>\n<section>\n<h2>8. Conclusion<\/h2>\n<p>\n            In this tutorial, we explored how to count leaf nodes in a binary tree. We confirmed that both recursive and iterative methods can be used to approach this problem. I hope this has helped deepen your understanding of tree data structures and DFS exploration algorithms. I want to emphasize that analyzing the performance of algorithms and considering efficiency is crucial in learning data structures and algorithms.\n        <\/p>\n<\/section>\n<\/article>\n","protected":false},"excerpt":{"rendered":"<p>1. Problem Definition The problem is to count the number of leaf nodes in a binary tree. A leaf node is a node that has no child nodes. Counting the number of nodes with no children is a good exercise for understanding the structure of the tree and utilizing exploration algorithms. To solve this problem, &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34618\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;JavaScript Coding Test Course, Counting the Number of Leaf Nodes&#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":[141],"tags":[],"class_list":["post-34618","post","type-post","status-publish","format-standard","hentry","category-javascript-coding-test"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>JavaScript Coding Test Course, Counting the Number of Leaf Nodes - \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\/34618\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JavaScript Coding Test Course, Counting the Number of Leaf Nodes - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"1. Problem Definition The problem is to count the number of leaf nodes in a binary tree. A leaf node is a node that has no child nodes. Counting the number of nodes with no children is a good exercise for understanding the structure of the tree and utilizing exploration algorithms. To solve this problem, &hellip; \ub354 \ubcf4\uae30 &quot;JavaScript Coding Test Course, Counting the Number of Leaf Nodes&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34618\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:30:10+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:40:28+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\/34618\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34618\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"JavaScript Coding Test Course, Counting the Number of Leaf Nodes\",\"datePublished\":\"2024-11-01T09:30:10+00:00\",\"dateModified\":\"2024-11-01T11:40:28+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34618\/\"},\"wordCount\":433,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Javascript Coding Test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34618\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34618\/\",\"name\":\"JavaScript Coding Test Course, Counting the Number of Leaf Nodes - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:30:10+00:00\",\"dateModified\":\"2024-11-01T11:40:28+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34618\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34618\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34618\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript Coding Test Course, Counting the Number of Leaf Nodes\"}]},{\"@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":"JavaScript Coding Test Course, Counting the Number of Leaf Nodes - \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\/34618\/","og_locale":"ko_KR","og_type":"article","og_title":"JavaScript Coding Test Course, Counting the Number of Leaf Nodes - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"1. Problem Definition The problem is to count the number of leaf nodes in a binary tree. A leaf node is a node that has no child nodes. Counting the number of nodes with no children is a good exercise for understanding the structure of the tree and utilizing exploration algorithms. To solve this problem, &hellip; \ub354 \ubcf4\uae30 \"JavaScript Coding Test Course, Counting the Number of Leaf Nodes\"","og_url":"https:\/\/atmokpo.com\/w\/34618\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:30:10+00:00","article_modified_time":"2024-11-01T11:40:28+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\/34618\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34618\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"JavaScript Coding Test Course, Counting the Number of Leaf Nodes","datePublished":"2024-11-01T09:30:10+00:00","dateModified":"2024-11-01T11:40:28+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34618\/"},"wordCount":433,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Javascript Coding Test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34618\/","url":"https:\/\/atmokpo.com\/w\/34618\/","name":"JavaScript Coding Test Course, Counting the Number of Leaf Nodes - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:30:10+00:00","dateModified":"2024-11-01T11:40:28+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34618\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34618\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34618\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"JavaScript Coding Test Course, Counting the Number of Leaf Nodes"}]},{"@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\/34618","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=34618"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34618\/revisions"}],"predecessor-version":[{"id":34619,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34618\/revisions\/34619"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34618"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34618"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34618"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}