{"id":8933,"date":"2021-11-29T00:14:24","date_gmt":"2021-11-29T08:14:24","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8933"},"modified":"2021-11-29T00:37:34","modified_gmt":"2021-11-29T08:37:34","slug":"leetcode-117-populating-next-right-pointers-in-each-node-ii","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-117-populating-next-right-pointers-in-each-node-ii\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 117. Populating Next Right Pointers in Each Node II"},"content":{"rendered":"\n<p>Given a binary tree<\/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 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2019\/02\/15\/117_sample.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> root = [1,2,3,4,5,null,7]\n<strong>Output:<\/strong> [1,#,2,3,#,4,5,7,#]\n<strong>Explanation: <\/strong>Given the above binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.\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>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, 6000]<\/code>.<\/li><li><code>-100 &lt;= Node.val &lt;= 100<\/code><\/li><\/ul>\n\n\n\n<p><strong>Follow-up:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>You may only use constant extra space.<\/li><li>The recursive approach is fine. You may assume 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 -2: Group nodes into levels<\/strong><\/h2>\n\n\n\n<p>Use pre-order traversal to group nodes by levels.<br>Connects nodes in each level.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  Node* connect(Node* root) {\n    vector<vector<Node*>> levels;\n    dfs(root, 0, levels);\n    for (auto& nodes : levels)\n      for(int i = 0; i < nodes.size() - 1; ++i)\n        nodes[i]->next = nodes[i+1]; \n    return root;\n  }\nprivate:\n  void dfs(Node *root, int d, vector<vector<Node*>>& levels) {\n    if (!root) return;\n    if (levels.size() < d+1) levels.push_back({});\n    levels[d].push_back(root);\n    dfs(root->left, d + 1, levels);\n    dfs(root->right, d + 1, levels);\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution -1: BFS level order traversal<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/p>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  Node* connect(Node* root) {\n    if (!root) return root;\n    queue<Node*> q{{root}};\n    while (!q.empty()) {\n      int size = q.size();\n      Node* p = nullptr;\n      while (size--) {\n        Node* t = q.front(); q.pop();\n        if (p) \n          p->next = t, p = p->next;\n        else\n          p = t;\n        if (t->left) q.push(t->left);\n        if (t->right) q.push(t->right);\n      }\n    }\n    return root;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: BFS w\/o extra space<\/strong><\/h2>\n\n\n\n<p>Populating the next level while traversing current level.<\/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\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  Node* connect(Node* root) {\n    Node* p = root;        \n    while (p) {\n      Node dummy;\n      Node* c = &dummy;\n      while (p) {\n        if (p->left)\n          c->next = p->left, c = c->next;\n        if (p->right)\n          c->next = p->right, c = c->next;\n        p = p->next;\n      }\n      p = dummy.next;\n    }\n    return root;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a binary tree struct Node { int val; Node *left; Node *right; Node *next; } Populate each next pointer to point to its next&#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":[542,177,32,28],"class_list":["post-8933","post","type-post","status-publish","format-standard","hentry","category-tree","tag-level-order","tag-medium","tag-traversal","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8933","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=8933"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8933\/revisions"}],"predecessor-version":[{"id":8936,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8933\/revisions\/8936"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8933"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8933"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8933"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}