{"id":5478,"date":"2019-08-22T08:58:13","date_gmt":"2019-08-22T15:58:13","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5478"},"modified":"2021-11-27T20:36:34","modified_gmt":"2021-11-28T04:36:34","slug":"leetcode-116-populating-next-right-pointers-in-each-node","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-116-populating-next-right-pointers-in-each-node\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 116. Populating Next Right Pointers in Each Node"},"content":{"rendered":"\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 116. Populating Next Right Pointers in Each Node - \u5237\u9898\u627e\u5de5\u4f5c EP265\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/YNu143ZN4qU?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>You are given a&nbsp;<strong>perfect binary tree<\/strong>&nbsp;where&nbsp;all leaves are on the same level, and every parent has two children. The binary tree has the following definition:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">struct Node {\n  int val;\n  Node *left;\n  Node *right;\n  Node *next;\n}\n<\/pre>\n\n\n\n<p>Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to&nbsp;<code>NULL<\/code>.<\/p>\n\n\n\n<p>Initially, all next pointers are set to&nbsp;<code>NULL<\/code>.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2019\/02\/14\/116_sample.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input: <\/strong>{\"$id\":\"1\",\"left\":{\"$id\":\"2\",\"left\":{\"$id\":\"3\",\"left\":null,\"next\":null,\"right\":null,\"val\":4},\"next\":null,\"right\":{\"$id\":\"4\",\"left\":null,\"next\":null,\"right\":null,\"val\":5},\"val\":2},\"next\":null,\"right\":{\"$id\":\"5\",\"left\":{\"$id\":\"6\",\"left\":null,\"next\":null,\"right\":null,\"val\":6},\"next\":null,\"right\":{\"$id\":\"7\",\"left\":null,\"next\":null,\"right\":null,\"val\":7},\"val\":3},\"val\":1}\n\n<strong>Output: <\/strong>{\"$id\":\"1\",\"left\":{\"$id\":\"2\",\"left\":{\"$id\":\"3\",\"left\":null,\"next\":{\"$id\":\"4\",\"left\":null,\"next\":{\"$id\":\"5\",\"left\":null,\"next\":{\"$id\":\"6\",\"left\":null,\"next\":null,\"right\":null,\"val\":7},\"right\":null,\"val\":6},\"right\":null,\"val\":5},\"right\":null,\"val\":4},\"next\":{\"$id\":\"7\",\"left\":{\"$ref\":\"5\"},\"next\":null,\"right\":{\"$ref\":\"6\"},\"val\":3},\"right\":{\"$ref\":\"4\"},\"val\":2},\"next\":null,\"right\":{\"$ref\":\"7\"},\"val\":1}\n\n<strong>Explanation: <\/strong>Given the above perfect binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B.\n<\/pre>\n\n\n\n<p><strong>Note:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>You may only use constant extra space.<\/li><li>Recursive approach is fine, implicit stack space does not count as extra space for this problem.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Recursion<\/strong><\/h2>\n\n\n\n<p>Do a preorder traversal:<br>1. return if self is empty or leaf<br>2. self.left-&gt;next = self.right<br>3. if self.next: self.right.next = self.next.left<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(log(n)) since it&#8217;s a perfect tree<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\/\/ Author: Huahua\nclass Solution {\npublic:\n  Node* connect(Node* root) {\n    if (!root || !root->left) return root;\n    root->left->next = root->right;\n    if (root->next)\n      root->right->next = root->next->left;\n    connect(root->left);\n    connect(root->right);\n    return root;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a&nbsp;perfect binary tree&nbsp;where&nbsp;all leaves are on the same level, and every parent has two children. The binary tree has the following definition:&#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":[491,177,17,28],"class_list":["post-5478","post","type-post","status-publish","format-standard","hentry","category-tree","tag-linkedlist","tag-medium","tag-recursion","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5478","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=5478"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5478\/revisions"}],"predecessor-version":[{"id":8832,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5478\/revisions\/8832"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5478"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5478"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5478"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}