{"id":320,"date":"2014-08-12T02:15:38","date_gmt":"2014-08-12T10:15:38","guid":{"rendered":"http:\/\/www.tech.dimprash.com\/?p=320"},"modified":"2017-04-15T14:23:22","modified_gmt":"2017-04-15T22:23:22","slug":"binary-tree-algorithms","status":"publish","type":"post","link":"http:\/\/www.tech.dimprash.com\/?p=320","title":{"rendered":"Binary Tree Algorithms"},"content":{"rendered":"<p>1) Find deepest node of a binary tree<br \/>\n   (Karumanchi, pg 120) <\/p>\n<p>2) Find height of a binary tree (recursion method)<br \/>\n   (Karumanchi, pg 119 , with small correction) <\/p>\n<p>3) Find height of binary tree (non recursion)<br \/>\n   (Karumanchi, pg 120, with small correction)<\/p>\n<p>4) Find Least Common Ancestor of 2 nodes in a binary tree<br \/>\n  (Karumanchi, pg 126)<br \/>\n<a href=\"https:\/\/www.youtube.com\/watch?v=13m9ZCB8gjw\">https:\/\/www.youtube.com\/watch?v=13m9ZCB8gjw<\/a><\/p>\n<p>5) Find Least Common Ancestor of 2 nodes in a binary SEARCH tree<br \/>\n  (Karumanchi, pg 153)<\/p>\n<p>6) Find Shortest path of 2 nodes in a binary SEARCH tree<br \/>\n  (Hint: Find LCA and then calculate path from LCA to each node)<\/p>\n<p>7) Determine if a Binary Tree is a Binary Search Tree<br \/>\nhttp:\/\/leetcode.com\/2010\/09\/determine-if-binary-tree-is-binary.html<br \/>\n(Karumanchi : pg 155, prob 52)<\/p>\n<p>8) Serialize and Deserialize a Binary Tree<br \/>\n<a href=\"http:\/\/www.geeksforgeeks.org\/serialize-deserialize-binary-tree\/\">http:\/\/www.geeksforgeeks.org\/serialize-deserialize-binary-tree\/<\/a><br \/>\n<a href=\"http:\/\/www.careercup.com\/question?id=6228581160058880\">http:\/\/www.careercup.com\/question?id=6228581160058880<\/a><br \/>\n<a href=\"http:\/\/leetcode.com\/2010\/09\/serializationdeserialization-of-binary.html\">http:\/\/leetcode.com\/2010\/09\/serializationdeserialization-of-binary.html<\/a><\/p>\n<p>  8 b) Serialize and Deserialize a Binary Search Tree<br \/>\n    <a href=\"https:\/\/www.youtube.com\/watch?v=H594EV9OuDI\">https:\/\/www.youtube.com\/watch?v=H594EV9OuDI<\/a><br \/>\n    <a href=\"http:\/\/leetcode.com\/2010\/09\/saving-binary-search-tree-to-file.html\">http:\/\/leetcode.com\/2010\/09\/saving-binary-search-tree-to-file.html<\/a><\/p>\n<p>9) Given a string of html tags like &#8220;< a >< b >< c >< \/c >< d >< \/d >< a >< \/a >< \/b >< \/a >&#8220;, construct a tree where each node is like<br \/>\nNode { string tag, ArrayOfChildren[] };<br \/>\nNote that the tree need not be binary tree.<br \/>\nThis was asked in SugarCRM. <\/p>\n<pre> \r\n\r\n\r\nBuildTree(root, parent) {\r\n   tag = readTag(); \/\/ assume function readTag will output each tag serially \r\n   if (isOpenTag(tag)) {\r\n       node = new Node;  \/\/ create a new Node\r\n       node.tag = tag; \r\n       Stack.push(node); \/\/ push into stack \r\n       if (root == NULL) { \r\n           root = node;  \r\n       } elseif (parent != NULL) {\r\n           AddChild(parent, node);\r\n           BuildTree(root, node);  \/\/ Build tree with node as parent \r\n       } else {\r\n          \/\/ error\r\n       }\r\n   } else {\r\n       temp = Stack.pop();\r\n       if (Stack.NotEmpty()) { \r\n          parent = Stack.pop; \r\n          BuildTree(root, parent);  \r\n       }\r\n   }\r\n   return root; \r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1) Find deepest node of a binary tree (Karumanchi, pg 120) 2) Find height of a binary tree (recursion method) (Karumanchi, pg 119 , with small correction) 3) Find height of binary tree (non recursion) (Karumanchi, pg 120, with small correction) 4) Find Least Common Ancestor of 2 nodes in a binary tree (Karumanchi, pg &hellip; <a href=\"http:\/\/www.tech.dimprash.com\/?p=320\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Binary Tree Algorithms<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-320","post","type-post","status-publish","format-standard","hentry","category-algorithms"],"_links":{"self":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/320","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=320"}],"version-history":[{"count":22,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/320\/revisions"}],"predecessor-version":[{"id":890,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/320\/revisions\/890"}],"wp:attachment":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=320"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=320"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=320"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}