Populating Next Right Pointers in Each Node II

Follow up for problem “Populating Next Right Pointers in Each Node“.

What if the given tree could be any binary tree? Would your previous solution still work?

Note:

  • You may only use constant extra space.

For example,
Given the following binary tree,

         1
       /  \
      2    3
     / \    \
    4   5    7

After calling your function, the tree should look like:

         1 -> NULL
       /  \
      2 -> 3 -> NULL
     / \    \
    4-> 5 -> 7 -> NULL
C++:
01 /**
02  * Definition for binary tree with next pointer.
03  * struct TreeLinkNode {
04  *  int val;
05  *  TreeLinkNode *left, *right, *next;
06  *  TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
07  * };
08  */
09 class Solution {
10 public:
11     void connect(TreeLinkNode *root) {
12         // Start typing your C/C++ solution below
13         // DO NOT write int main() function
14         vector<TreeLinkNode*> A;
15         vector<TreeLinkNode*> B;
16         A.push_back(root);
17         while(A.size()!=0)
18         {
19             for(int i=0;i<A.size();i++)
20             {
21                 if(A[i]!=NULL)
22                 {
23                     if(A[i]->left!=NULL)
24                     {
25                         B.push_back(A[i]->left);
26                     }
27                     if(A[i]->right!=NULL)
28                     {
29                         B.push_back(A[i]->right);
30                     }
31                 }
32             }
33             int t=B.size()1;
34             for(int i=0;i<t;i++)
35             {
36                 B[i]->next=B[i+1];
37             }
38             A=B;
39             B.clear();
40         }
41     }
42 };
一种简单的recursive解法:
C++:
01 /**
02  * Definition for binary tree with next pointer.
03  * struct TreeLinkNode {
04  *  int val;
05  *  TreeLinkNode *left, *right, *next;
06  *  TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
07  * };
08  */
09  TreeLinkNode* FindNext(TreeLinkNode* root)
10  {
11      if(root==NULL)
12      {
13          return NULL;
14      }
15      if(root->left!=NULL)
16      {
17          return root->left;
18      }
19      if(root->right!=NULL)
20      {
21          return root->right;
22      }
23      return FindNext(root->next);
24  }
25 void connectHelper(TreeLinkNode* root, TreeLinkNode* next)
26 {
27     if(root==NULL)
28         return;
29 
30     root->next=next;
31     TreeLinkNode* tmp=FindNext(next);
32     connectHelper(root->right, tmp);
33     if(root->right)
34     {
35         connectHelper(root->left, root->right);
36     }
37     else
38     {
39         connectHelper(root->left, tmp);
40     }
41 
42 }
43 class Solution {
44 public:
45     void connect(TreeLinkNode *root) {
46         // Start typing your C/C++ solution below
47         // DO NOT write int main() function
48         connectHelper(root, NULL);
49     }
50 };
Advertisements
This entry was posted in Array and linked list, Stack and Queue, Tree. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s