# Middle-题目17：116. Populating Next Right Pointers in Each Node

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

public class Solution {
if(root==null)
return;
else if(root.left == null && root.right == null && root.next == null) { // root is a leaf node
root.next=null;
return;
}
else {
if(root.left != null)
root.left.next = root.right;
if(root.right != null && root.next != null)
root.right.next = root.next.left;
if(root.right != null && root.next == null)
root.right.next = null;
connect(root.left);
connect(root.right);
}
}
}

0ms，beats 81.60%，众数1ms，52.14%

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

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