题目：Given a binary tree

    struct TreeLinkNode {
}


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.
• You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children).

For 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

代码：

/**
* 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 ;
if(root->left&&root->right){
root->left->next=root->right;
}
if(root->next&&root->left){//到了非根节点的时候
root->right->next=root->next->left;
}

connect(root->left);connect(root->right);
}
*/
if(root==NULL)  return ;

while(root&&root->left){
while(root){
root->left->next=root->right;
if(root->next){
root->right->next=root->next->left;
}
root=root->next;
}
root=tmp;
}
}
};

LeetCode 116 Populating Next Right Pointers in Each Node 解题报告

2015-03-30 20:44:13

Populating Next Right Pointers in Each Node -- LeetCode

2014-04-12 03:11:29

117. Populating Next Right Pointers in Each Node II Leetcode Python

2015-01-26 10:16:40

Populating Next Right Pointers in Each Node II 任意（非完美）二叉树添加next指针 @LeetCode

2013-11-21 04:45:02

LeetCode题解：Populating Next Right Pointers in Each Node

2015-10-02 21:01:47

Populating Next Right Pointers in Each Node II -- LeetCode

2014-04-13 09:09:24

Leetcode_116_Populating Next Right Pointers in Each Node

2015-02-05 19:18:29

【LeetCode with Python】 Populating Next Right Pointers in Each Node

2014-07-04 16:26:33

LeetCode（117） Populating Next Right Pointers in Each Node II

2015-11-14 15:13:02

树——populating-next-right-pointers-in-each-node（层序遍历变型）

2016-05-10 22:29:25