# 题目：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

# 代码：

/**
* Definition for binary tree with next pointer.
*  int val;
*  TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
* };
*/
class Solution {
public:

if(root==NULL)  return;

while(root!=NULL){
while(root!=NULL){
if(root->left){
tail->next=root->left;
tail=tail->next;
}
if(root->right){
tail->next=root->right;
tail=tail->next;
}
root=root->next;
}
root=dummy->next;
dummy->next=NULL;//如果没有这句话，那么dummy只是上面那一层的
}
delete dummy;
}
};

• 本文已收录于以下专栏：

举报原因： 您举报文章：Populating Next Right Pointers in Each Node II 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)