题目：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;
}
};

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

leetcode 之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...

Populating Next Right Pointers in Each Node II (Java)

Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tree could...

LeetCode刷题笔录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...

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

LeetCode 117 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...

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

Leetcode 117. Populating Next Right Pointers in Each Node II 链接邻居2 解题报告

1 解题思想这道题和116基本一样： Leetcode 116. Populating Next Right Pointers in Each Node 链接邻居 解题报告 区别在于这个是任意二叉树...
• MebiuW
• 2016-09-30 22:52
• 1050

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

举报原因： 您举报文章：深度学习：神经网络中的前向传播和反向传播算法推导 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)