{"id":11466,"date":"2022-05-22T17:08:00","date_gmt":"2022-05-22T17:08:00","guid":{"rendered":"https:\/\/www.softwareeverydayblog.com\/?p=11466"},"modified":"2022-05-22T17:09:13","modified_gmt":"2022-05-22T17:09:13","slug":"programming-problem-flatten-binary-tree-to-linked-list","status":"publish","type":"post","link":"https:\/\/www.softwareeverydayblog.com\/?p=11466","title":{"rendered":"[Programming Problem] Flatten Binary Tree to Linked List"},"content":{"rendered":"<p>Given the root of a binary tree, flatten the tree into a &#8220;linked list&#8221;:<\/p>\n<p>&#8211; The &#8220;linked list&#8221; should use the same TreeNode class where the right child pointer points to the next node in the list and the left child pointer is always null.<\/p>\n<p>&#8211; The &#8220;linked list&#8221; should be in the same order as a pre-order traversal of the binary tree.<\/p>\n<p>[<a href=\"https:\/\/leetcode.com\/problems\/flatten-binary-tree-to-linked-list\/\" rel=\"noopener\" target=\"_blank\">Problem Link<\/a>]<\/p>\n<p>Recursively run through the whole tree. During return of the recursion, root.right is set to left_linked_list (return from left recursion) and the end of left_linked_list.right is set to right_linked_list (return from right recursion). One gotcha, is the need to account for left_linked_list being empty\/null. Also, don&#8217;t forget to set root.left to null.<\/p>\n<pre lang=\"javascript\">\r\n\/**\r\n * Definition for a binary tree node.\r\n * function TreeNode(val, left, right) {\r\n *     this.val = (val===undefined ? 0 : val)\r\n *     this.left = (left===undefined ? null : left)\r\n *     this.right = (right===undefined ? null : right)\r\n * }\r\n *\/\r\n\/**\r\n * @param {TreeNode} root\r\n * @return {void} Do not return anything, modify root in-place instead.\r\n *\/\r\n\r\nvar lastEl = function(root) {\r\n    let curr = root;\r\n    while (curr.right !== null) curr = curr.right;\r\n    return curr;\r\n}\r\n\r\n\r\nvar flatten = function(root) {\r\n    if (!root) return null;\r\n    \r\n    let leftLL = flatten(root.left);\r\n    let rightLL = flatten(root.right);\r\n    \r\n    root.left = null;\r\n    \/\/root.right = null;\r\n    \r\n    if (leftLL) {\r\n        root.right = leftLL;\r\n        lastEl(leftLL).right = rightLL;\r\n    } else {\r\n        root.right = rightLL;\r\n    }\r\n    \r\n    return root;\r\n};\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Given the root of a binary tree, flatten the tree into a &#8220;linked list&#8221;: &#8211; The &#8220;linked list&#8221; should use the same TreeNode class where the right child pointer points to the next node in the list and the left child pointer is always null. &#8211; The &#8220;linked list&#8221; should be in the same order [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-11466","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/posts\/11466","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=11466"}],"version-history":[{"count":3,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/posts\/11466\/revisions"}],"predecessor-version":[{"id":11468,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/posts\/11466\/revisions\/11468"}],"wp:attachment":[{"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=11466"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=11466"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=11466"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}