{"id":20341,"date":"2022-11-02T22:02:11","date_gmt":"2022-11-02T16:32:11","guid":{"rendered":"https:\/\/copyassignment.com\/?p=20341"},"modified":"2022-11-03T12:18:21","modified_gmt":"2022-11-03T06:48:21","slug":"hackerrank-day-22-solution-in-python-binary-search-trees","status":"publish","type":"post","link":"https:\/\/copyassignment.com\/hackerrank-day-22-solution-in-python-binary-search-trees\/","title":{"rendered":"HackerRank Day 22 Solution in Python: Binary Search Trees"},"content":{"rendered":"\n<p>Today we will see the&nbsp;<strong><em>HackerRank Day 22 Solution in Python<\/em><\/strong>. The problem is named&nbsp;<strong><em>Binary Search Trees<\/em><\/strong>&nbsp;which is part of&nbsp;<strong><em>30 Days of code on HackerRank<\/em><\/strong>. Let\u2019s get started!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Day 22: Binary Search Trees Problem statement<\/h2>\n\n\n\n<p>We are given a pointer, pointing to the root of a binary search tree. We have to complete the&nbsp;<em>getHeight<\/em>&nbsp;function provided so that it returns the height of the binary search tree. The height of a binary search tree is the number of edges between the tree&#8217;s root and its furthest leaf. <\/p>\n\n\n\n<p><strong>Sample Input<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>7\n3\n5\n2\n1\n4\n6\n7<\/code><\/pre>\n\n\n\n<p><strong>Sample Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>3<\/code><\/pre>\n\n\n\n<p><strong>Explanation:<\/strong> The height of the given binary tree is 3<\/p>\n\n\n\n<p>You can solve the problem&nbsp;<a href=\"https:\/\/www.hackerrank.com\/challenges\/30-2d-arrays\/problem\" target=\"_blank\" rel=\"noreferrer noopener\">h<\/a><a href=\"https:\/\/www.hackerrank.com\/challenges\/30-binary-search-trees\/problem\" target=\"_blank\" data-type=\"URL\" data-id=\"https:\/\/www.hackerrank.com\/challenges\/30-binary-search-trees\/problem\" rel=\"noreferrer noopener\">ere<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">HackerRank Day 22 Solution in Python<\/h2>\n\n\n\n<script async=\"\" src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\n<ins class=\"adsbygoogle\" style=\"display:block; text-align:center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-9886351916045880\" data-ad-slot=\"2002566052\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><div style=\"position:absolute;top:-20px;right:0px;cursor:pointer\" class=\"copy-simple-code-block\"><span class=\"dashicon dashicons dashicons-admin-page\"><\/span><\/div><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"python\" data-theme=\"xcode\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"true\">class Node:\n    def __init__(self,data):\n        self.right=self.left=None\n        self.data = data\n        \nclass Solution:\n    #Method to insert new data\n    def insert(self,root,data):\n        if root==None:\n            return Node(data)\n        else:\n            if data&lt;=root.data:\n                cur=self.insert(root.left,data)\n                root.left=cur\n            else:\n                cur=self.insert(root.right,data)\n                root.right=cur\n        return root\n    \n    #Function to get height of the tree\n    def getHeight(self,root):\n        #If root is null then return -1\n        if root is None:\n            return -1\n        #recursive function call to calculate the height\n        left = self.getHeight(root.left)\n        right = self.getHeight(root.right)\n        return max(left, right) + 1\n        \nT=int(input())\nmyTree=Solution()\nroot=None\n\n#get input from the user for the tree\nfor i in range(T):\n    data=int(input())\n    root=myTree.insert(root,data)\n\n#Call the getHeight method\nheight=myTree.getHeight(root)\n#Print the height of the tree\nprint(height)<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Code Explanation<\/h2>\n\n\n\n<script async=\"\" src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\n<ins class=\"adsbygoogle\" style=\"display:block; text-align:center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-9886351916045880\" data-ad-slot=\"2002566052\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n\n\n<ul class=\"wp-block-list\">\n<li>We have created getHeight method inside the Solution class<\/li>\n\n\n\n<li>In the getHeight method, if the root is null, then return -1<\/li>\n\n\n\n<li>Else use a recursive function call to traverse through the tree<\/li>\n\n\n\n<li>After reaching the end return the maximum value(left or right)<\/li>\n<\/ul>\n\n\n\n<div style=\"text-align:center\" class=\"wp-block-atomic-blocks-ab-button ab-block-button\"><a href=\"https:\/\/copyassignment.com\/top-100-python-projects-with-source-code\/\" class=\"ab-button ab-button-shape-rounded ab-button-size-medium\" style=\"color:#ffffff;background-color:#3373dc\">Best 100+ Python Projects with source code<\/a><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>Also Read:<\/strong><\/p>\n\n\n<ul class=\"wp-block-latest-posts__list is-grid columns-3 wp-block-latest-posts\"><li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-8-solution-in-python-dictionaries-and-maps\/\">HackerRank Day 8 Solution in Python: Dictionaries and Maps<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-7-solution-in-python-arrays\/\">HackerRank Day 7 Solution in Python: Arrays<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-6-solution-in-python-lets-review\/\">HackerRank Day 6 Solution in Python: Let&#8217;s review<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-5-solution-in-python-loops\/\">HackerRank Day 5 Solution in Python: Loops<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-4-solution-in-python\/\">HackerRank Day 4 Solution in Python: Class vs Instance<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-3-solution-in-python\/\">HackerRank Day 3 Solution in Python: Intro to Conditional Statements<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-2-solution-in-python-operators\/\">HackerRank Day 2 Solution in Python: Operators<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-1-solution-in-python-data-types\/\">HackerRank Day 1 Solution in Python: Data Types<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-0-solution-in-python-hello-world\/\">HackerRank Day 0 Solution in Python: Hello World<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-29-solution-in-python-bitwise-and\/\">HackerRank Day 29 Solution in Python: Bitwise AND<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-28-solution-in-python-regex-patterns-and-intro-to-databases\/\">HackerRank Day 28 Solution in Python: RegEx, Patterns, and Intro to databases<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-27-solution-in-python-testing\/\">HackerRank Day 27 Solution in Python: Testing<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-26-solution-in-python-nested-logic\/\">HackerRank Day 26 Solution in Python: Nested Logic<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-25-solution-in-python-running-time-and-complexity\/\">HackerRank Day 25 Solution in Python: Running Time and Complexity<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-24-solution-in-python-more-linked-lists\/\">HackerRank Day 24 Solution in Python: More Linked Lists<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-23-solution-in-python-bst-level-order-traversal\/\">HackerRank Day 23 Solution in Python: BST Level Order Traversal<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-22-solution-in-python-binary-search-trees\/\">HackerRank Day 22 Solution in Python: Binary Search Trees<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-20-solution-in-python-sorting\/\">HackerRank Day 20 Solution in Python: Sorting<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-19-solution-in-python-interfaces\/\">HackerRank Day 19 Solution in Python: Interfaces<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-18-solution-in-python-queues-and-stacks\/\">HackerRank Day 18 Solution in Python: Queues and Stacks<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-17-solution-in-python-more-exceptions\/\">HackerRank Day 17 Solution in Python: More Exceptions<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-16-solution-exceptions-string-to-integer\/\">HackerRank Day 16 Solution: Exceptions &#8211; String to Integer<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-15-solution-in-python-linked-list\/\">HackerRank Day 15 Solution in Python: Linked List<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-13-solution-in-python-abstract-classes\/\">HackerRank Day 13 Solution in Python: Abstract Classes<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-14-solution-in-python-scope\/\">HackerRank Day 14 Solution in Python: Scope<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-12-solution-in-python-inheritance\/\">HackerRank Day 12 Solution in Python: Inheritance<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-11-solution-in-python-2d-arrays\/\">HackerRank Day 11 Solution in Python: 2D Arrays<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-10-solution-in-python-binary-numbers\/\">HackerRank Day 10 Solution in Python: Binary Numbers<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>Today we will see the&nbsp;HackerRank Day 22 Solution in Python. The problem is named&nbsp;Binary Search Trees&nbsp;which is part of&nbsp;30 Days of code on HackerRank. Let\u2019s&#8230;<\/p>\n","protected":false},"author":62,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1932,22,1306],"tags":[],"class_list":["post-20341","post","type-post","status-publish","format-standard","hentry","category-30-days-of-code","category-allcategorites","category-competitive-programming","wpcat-1932-id","wpcat-22-id","wpcat-1306-id"],"_links":{"self":[{"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/posts\/20341","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/users\/62"}],"replies":[{"embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/comments?post=20341"}],"version-history":[{"count":0,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/posts\/20341\/revisions"}],"wp:attachment":[{"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/media?parent=20341"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/categories?post=20341"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/tags?post=20341"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}