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

Populating Next Right Pointers in Each Node II   What if the given tree could be any binary tree? Wo...

## [LeetCode]problem 117. Populating Next Right Pointers in Each Node II

TAG层序遍历 - 常量空间，通过next指针二叉树link方法有了Populating next right pointers in each node I的铺垫，这道题就显得没有任何思维上的限制了...

## 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 -- Populating Next Right Pointers in Each Node I&II -- 再看简单常考

Populating Next Right Pointers in Each Nodehttps://leetcode.com/problems/populating-next-right-point...
• xyqzki
• 2015年12月16日 22:05
• 266

## Populating Next Right Pointers in Each Node II

/** * Definition for binary tree with next pointer. * public class TreeLinkNode { * int val; ...

## LeetCode_Tree_Populating Next Right Pointers in Each Node I II

Populating Next Right Pointers in Each Node I一、题目说明如图所说，将二叉树中每一层作指向。二、解题思路按层级遍历树的节点，使得横向前节点指向后节点。然后再...
• glDemo
• 2015年09月06日 21:13
• 434

## populating-next-right-pointers-in-each-node II

I Populate each next pointer to point to its next right node. If there is no next right node, the ne...
• ld851
• 2017年08月08日 21:49
• 50

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