

{"id":152,"date":"2022-03-05T19:44:28","date_gmt":"2022-03-06T00:44:28","guid":{"rendered":"https:\/\/sites.temple.edu\/vahid\/?p=152"},"modified":"2022-03-05T19:44:29","modified_gmt":"2022-03-06T00:44:29","slug":"hackerrank-solutions-tree-preorder-traversal","status":"publish","type":"post","link":"https:\/\/sites.temple.edu\/vahid\/2022\/03\/05\/hackerrank-solutions-tree-preorder-traversal\/","title":{"rendered":"HackerRank Solutions: Tree: Preorder Traversal"},"content":{"rendered":"\n<p class=\"has-text-align-center\"><strong>Problem<\/strong><\/p>\n\n\n\n<p>Complete the&nbsp;&nbsp;function in the editor below, which has&nbsp;&nbsp;parameter: a pointer to the root of a binary tree. It must print the values in the tree&#8217;s preorder traversal as a single line of space-separated values.<\/p>\n\n\n\n<p><strong>Input Format<\/strong><\/p>\n\n\n\n<p>Our test code passes the root node of a binary tree to the&nbsp;<em>preOrder<\/em>&nbsp;function.<\/p>\n\n\n\n<p><strong>Constraints<\/strong><\/p>\n\n\n\n<p>&nbsp;Nodes in the tree&nbsp;<\/p>\n\n\n\n<p><strong>Output Format<\/strong><\/p>\n\n\n\n<p>Print the tree&#8217;s preorder traversal as a single line of space-separated values.<\/p>\n\n\n\n<p><strong>Sample Input<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>     1\n      \\\n       2\n        \\\n         5\n        \/  \\\n       3    6\n        \\\n         4  \n<\/code><\/pre>\n\n\n\n<p><strong>Sample Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1 2 5 3 4 6 \n<\/code><\/pre>\n\n\n\n<p><strong>Explanation<\/strong><\/p>\n\n\n\n<p>The preorder traversal of the binary tree is printed.<\/p>\n\n\n\n<p class=\"has-text-align-center\"><strong>Solution<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Node:\r\n    def __init__(self, info): \r\n        self.info = info  \r\n        self.left = None  \r\n        self.right = None \r\n        self.level = None \r\n\r\n    def __str__(self):\r\n        return str(self.info) \r\n\r\nclass BinarySearchTree:\r\n    def __init__(self): \r\n        self.root = None\r\n\r\n    def create(self, val):  \r\n        if self.root == None:\r\n            self.root = Node(val)\r\n        else:\r\n            current = self.root\r\n         \r\n            while True:\r\n                if val &lt; current.info:\r\n                    if current.left:\r\n                        current = current.left\r\n                    else:\r\n                        current.left = Node(val)\r\n                        break\r\n                elif val &gt; current.info:\r\n                    if current.right:\r\n                        current = current.right\r\n                    else:\r\n                        current.right = Node(val)\r\n                        break\r\n                else:\r\n                    break\r\n\r\n\"\"\"\r\nNode is defined as\r\nself.left (the left child of the node)\r\nself.right (the right child of the node)\r\nself.info (the value of the node)\r\n\"\"\"\r\ndef preOrder(root):\r\n    #Write your code here\r\n    stack = &#091;root]\r\n    sti = 0\r\n    while (sti &lt; len(stack)):\r\n        currnode = stack&#091;sti]\r\n        if currnode.left:\r\n            stack.insert(sti+1, currnode.left)\r\n            if currnode.right:\r\n                stack.insert(sti+2, currnode.right)\r\n        elif currnode.right:\r\n            stack.insert(sti+1, currnode.right)\r\n        sti += 1\r\n    for s in stack:\r\n        print(s.info, end=' ')\r\n\r<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Problem Complete the&nbsp;&nbsp;function in the editor below, which has&nbsp;&nbsp;parameter: a pointer to the root of a binary tree. It must print the values in the&#8230;<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"https:\/\/sites.temple.edu\/vahid\/2022\/03\/05\/hackerrank-solutions-tree-preorder-traversal\/\">Continue reading<span class=\"screen-reader-text\">HackerRank Solutions: Tree: Preorder Traversal<\/span><\/a><\/div>\n","protected":false},"author":18759,"featured_media":154,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[102,4,103],"class_list":["post-152","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programming","tag-hackerrank","tag-python","tag-solution","entry"],"_links":{"self":[{"href":"https:\/\/sites.temple.edu\/vahid\/wp-json\/wp\/v2\/posts\/152","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sites.temple.edu\/vahid\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sites.temple.edu\/vahid\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sites.temple.edu\/vahid\/wp-json\/wp\/v2\/users\/18759"}],"replies":[{"embeddable":true,"href":"https:\/\/sites.temple.edu\/vahid\/wp-json\/wp\/v2\/comments?post=152"}],"version-history":[{"count":0,"href":"https:\/\/sites.temple.edu\/vahid\/wp-json\/wp\/v2\/posts\/152\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sites.temple.edu\/vahid\/wp-json\/wp\/v2\/media\/154"}],"wp:attachment":[{"href":"https:\/\/sites.temple.edu\/vahid\/wp-json\/wp\/v2\/media?parent=152"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sites.temple.edu\/vahid\/wp-json\/wp\/v2\/categories?post=152"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sites.temple.edu\/vahid\/wp-json\/wp\/v2\/tags?post=152"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}