{"id":8828,"date":"2021-11-27T20:23:33","date_gmt":"2021-11-28T04:23:33","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8828"},"modified":"2021-11-27T20:34:20","modified_gmt":"2021-11-28T04:34:20","slug":"leetcode-114-flatten-binary-tree-to-linked-list","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-114-flatten-binary-tree-to-linked-list\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 114. Flatten Binary Tree to Linked List"},"content":{"rendered":"\n<p>Given the&nbsp;<code>root<\/code>&nbsp;of a binary tree, flatten the tree into a &#8220;linked list&#8221;:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The &#8220;linked list&#8221; should use the same&nbsp;<code>TreeNode<\/code>&nbsp;class where the&nbsp;<code>right<\/code>&nbsp;child pointer points to the next node in the list and the&nbsp;<code>left<\/code>&nbsp;child pointer is always&nbsp;<code>null<\/code>.<\/li><li>The &#8220;linked list&#8221; should be in the same order as a&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Tree_traversal#Pre-order,_NLR\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>pre-order<\/strong><strong>&nbsp;traversal<\/strong><\/a>&nbsp;of the binary tree.<\/li><\/ul>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/01\/14\/flaten.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> root = [1,2,5,3,4,null,6]\n<strong>Output:<\/strong> [1,null,2,null,3,null,4,null,5,null,6]\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> root = []\n<strong>Output:<\/strong> []\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> root = [0]\n<strong>Output:<\/strong> [0]\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The number of nodes in the tree is in the range&nbsp;<code>[0, 2000]<\/code>.<\/li><li><code>-100 &lt;= Node.val &lt;= 100<\/code><\/li><\/ul>\n\n\n\n<p><strong>Follow up:<\/strong>&nbsp;Can you flatten the tree in-place (with&nbsp;<code>O(1)<\/code>&nbsp;extra space)?<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: Recursion<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(|height|)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\"># Author: Huahua\nclass Solution:\n  def flatten(self, root: Optional[TreeNode]) -&gt; None:\n    \"\"\"\n    Do not return anything, modify root in-place instead.\n    \"\"\"\n    def solve(root: Optional[TreeNode]):\n      if not root: return None, None\n      if not root.left and not root.right: return root, root\n      l_head = l_tail = r_head = r_tail = None\n      if root.left:\n        l_head, l_tail = solve(root.left)\n      if root.right:\n        r_head, r_tail = solve(root.right)\n      root.left = None\n      root.right = l_head or r_head\n      if l_tail:\n        l_tail.right = r_head\n      return root, r_tail or l_tail\n    return solve(root)[0]\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><strong>Solution 2: Unfolding<\/strong><\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\"># Author: Huahua\nclass Solution:\n  def flatten(self, root: Optional[TreeNode]) -> None:\n    while root:\n      if root.left:\n        prev = root.left\n        while prev.right: prev = prev.right\n        prev.right = root.right\n        root.right = root.left\n        root.left = None\n      root = root.right\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given the&nbsp;root&nbsp;of a binary tree, flatten the tree into a &#8220;linked list&#8221;: The &#8220;linked list&#8221; should use the same&nbsp;TreeNode&nbsp;class where the&nbsp;right&nbsp;child pointer points to the&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[45],"tags":[83,177,28],"class_list":["post-8828","post","type-post","status-publish","format-standard","hentry","category-tree","tag-list","tag-medium","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8828","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=8828"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8828\/revisions"}],"predecessor-version":[{"id":8831,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8828\/revisions\/8831"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8828"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8828"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8828"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}