1. 程式人生 > >[C++]LeetCode116. 填充同一層的兄弟節點 | Populating Next Right Pointers in Each Node

[C++]LeetCode116. 填充同一層的兄弟節點 | Populating Next Right Pointers in Each Node

Given a binary tree

struct TreeLinkNode {
  TreeLinkNode *left;
  TreeLinkNode *right;
  TreeLinkNode *next;
}

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 NULL.

Initially, all next pointers are set to NULL

.

Note:

  • You may only use constant extra space.
  • Recursive approach is fine, implicit stack space does not count as extra space for this problem.
  • You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children).

Example:

Given the following perfect binary tree,

     1
   /  \
  2    3
 / \  / \
4  5  6  7

After calling your function, the tree should look like:

     1 -> NULL
   /  \
  2 -> 3 -> NULL
 / \  / \
4->5->6->7 -> NULL

給定一個二叉樹

struct TreeLinkNode {
  TreeLinkNode *left;
  TreeLinkNode *right;
  TreeLinkNode *next;
}

填充它的每個 next 指標,讓這個指標指向其下一個右側節點。如果找不到下一個右側節點,則將 next 指標設定為 NULL

初始狀態下,所有 next 指標都被設定為 NULL

說明:

  • 你只能使用額外常數空間。
  • 使用遞迴解題也符合要求,本題中遞迴程式佔用的棧空間不算做額外的空間複雜度。

示例:

給定二叉樹,

     1
   /  \
  2    3
 / \    \
4   5    7

呼叫你的函式後,該二叉樹變為:

     1 -> NULL
   /  \
  2 -> 3 -> NULL
 / \    \
4-> 5 -> 7 -> NULL

8ms
 1 /**
 2  * Definition for binary tree with next pointer.
 3  * struct TreeLinkNode {
 4  *  int val;
 5  *  TreeLinkNode *left, *right, *next;
 6  *  TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     void connect(TreeLinkNode *root) {
12         if(root == NULL) return;
13         if(root->next && root->right)
14             root->right->next = root->next->left;
15         if(root->right)
16             root->left->next = root->right;
17         connect(root->left);
18         connect(root->right);
19     }
20 };

12ms

 1 /**
 2  * Definition for binary tree with next pointer.
 3  * struct TreeLinkNode {
 4  *  int val;
 5  *  TreeLinkNode *left, *right, *next;
 6  *  TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     void connect(TreeLinkNode *root) 
12     {
13         vector<TreeLinkNode*> tmp;
14         queue<TreeLinkNode*> q;
15         if(!root){return ;};
16         q.push(root);
17         q.push(NULL);
18         
19         while(!q.empty())
20         {
21             TreeLinkNode* node = q.front();
22             q.pop();
23             
24             if(!node)
25             {
26                 linker(tmp);
27                 tmp.clear();
28                 if(q.size() > 0){q.push(NULL); };
29                 
30             }else{
31                 
32                 tmp.push_back(node);
33                 if(node -> left != NULL){q.push(node -> left); };
34                 if(node -> right != NULL){q.push(node -> right); };
35                 
36             }
37         }
38         
39     }
40     
41     void linker(vector<TreeLinkNode*>& nodes)
42     {
43         for(int i = 0; i < nodes.size() - 1; ++i)
44         {
45             nodes[i] -> next = nodes[i + 1];
46         }
47         
48         nodes.back() -> next = NULL;
49     }
50 };

16ms

 1 /**
 2  * Definition for binary tree with next pointer.
 3  * struct TreeLinkNode {
 4  *  int val;
 5  *  TreeLinkNode *left, *right, *next;
 6  *  TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     void connect(TreeLinkNode *root) {
12         if (!root)
13             return;
14         //佇列
15         queue<TreeLinkNode*> q;
16         q.push(root);
17         while (!q.empty())
18         {
19             int len = q.size();
20             cout << "len:" << len << endl;
21             for (int i = 0; i < len; i++)
22             {
23                 TreeLinkNode* cur_node = q.front();
24                 q.pop();
25                 //當前結點的next,就是q在pop後的front結點
26                 //注意:需要用len-1判斷,而不能用q.empty()判斷,因為q是在動態增長的
27                 if (i < len - 1)
28                 {
29                     cur_node->next = q.front();                    
30                 }
31                 //左右子樹入佇列
32                 if (cur_node->left)
33                 {
34                     q.push(cur_node->left);
35                 }
36                 if (cur_node->right)
37                 {
38                     q.push(cur_node->right);
39                 }
40             }
41         }
42         return;
43     }
44 };